KANIKIG

KANIKIG

just for fun | 兴趣使然 Ph.D. in Engineering|❤️ #NFT $ETH| [Twitter](https://twitter.com/kanikig2)|[Github](https://github.com/KANIKIG)|[Telegram channel](https://t.me/kanikigtech)

Introduction to Regular Expressions

image

Reference: https://deerchao.cn/tutorials/regex/regex.htm

What is a Regular Expression#

A regular expression is code that records text rules.

Testing Regular Expressions#

Online testing tool: wegester

Metacharacters#

CodeDescription
.Matches any character except a newline
\wMatches a letter, digit, underscore, or Chinese character
\sMatches any whitespace character
\dMatches a digit
\bMatches the beginning or end of a word
^Matches the beginning of a string
$Matches the end of a string

Example:

TargetRegular Expression
A Lucy following not far behind hi\bhi\b.*\bLucy\b
Starts with 0, followed by two digits, then a hyphen "-", and finally eight digits0\d\d-\d\d\d\d\d\d\d\d or 0\d{2}-\d{8}
Matches words starting with the letter a\ba\w*\b
One or more consecutive digits\d+
Exactly six-character words\b\w{6}\b
The entire string is 5 to 12 digits^\d{5,12}$

Character Escaping#

Example:

TargetRegular Expression
deerchao.cndeerchao\.cn
C:\WindowsC:\\Windows

Repetition#

Code/SyntaxDescription
*Matches zero or more times
+Matches one or more times
?Matches zero or one time
{n}Matches exactly n times
{n,}Matches n or more times
{n,m}Matches between n and m times

Example:

Regular ExpressionTarget
Windows\d+Windows followed by one or more digits
^\w+The first word of a line

Character Classes#

Example:

Regular ExpressionTarget
[aeiou]Any one English vowel letter
[0-9]One digit
(?0\d{2}[) -]?\d{8}Several formats of phone numbers

Branch Conditions#

When matching branch conditions, each condition will be tested from left to right. If one branch is satisfied, the other conditions will not be considered.

Example:

Regular ExpressionTarget
0\d{2}-\d{8}|0\d{3}-\d{7}Three-digit area code, followed by an eight-digit local number or a four-digit area code, followed by a seven-digit local number
\d{5}-\d{4}|\d{5}The rule for US zip codes is either five digits or nine digits separated by a hyphen

Grouping#

You can use parentheses to specify subexpressions (also known as groups).

Example:

Regular ExpressionTarget
(\d{1,3}.){3}\d{1,3}Simple IP address matching
((2[0-4]\d|25[0-5]|[01]?\d\d?).){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)Correct IP address

Negation#

CodeDescription
\WMatches any character that is not a letter, digit, underscore, or Chinese character
\SMatches any character that is not a whitespace character
\DMatches any character that is not a digit
\BMatches a position that is not the beginning or end of a word
[^x]Matches any character except x
[^aeiou]Matches any character except the specified vowels

Example:

Regular ExpressionTarget
\S+Strings without whitespace
<a[^>]+>Strings starting with "a" enclosed in angle brackets

Backreferences#

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.