metacharacters
\b is a special code specified by the regular expression that represents the beginning or end of a word, which is the boundary of a word
. Is a metacharacter that matches any character except newline
* Is also a metacharacter, but it means that the preceding content can be repeated any number of times in a row to make the entire expression match
\d metacharacter, matching one digit (0, or 1, or 2, or…)
\d+ Matches 1 or more consecutive numbers. + is the same as the similar metacharacter, except that it matches any number of repetitions (possibly zero), whereas + matches one or more repetitions
– Not a metacharacter, matches only its own hyphen (or minus, or dash)
0 \ d {2} – \ d {8}. {2}({8}) = {2}({8}) = {2}({8})
\s Matches any whitespace characters, including Spaces, tabs, newlines, Chinese full-corner Spaces, etc
\w Matches letters or numbers or underscores or Chinese characters
^ Match the beginning of the string you are looking for,
$matches the end of the string you are looking for
^\d{5,12}$The entire input must be 5 to 12 digits
Character escaping
If you want to look for metacharacters themselves, for example, if you look for., or *, there’s a problem: you can’t specify them because they’ll be interpreted to mean something else. In this case, you have to use \ to desecrate these characters
Mengxiangyu \. Xyz matching mengxiangyu. Xyz
repeat
* Repeat 0 or more times
+ Repeat once or more
? Repeat 0 or 1 times
{n} repeat n times
{n,} Repeat n or more times
{n,m} repeat n to m times
Hello \d+ matches hello followed by one or more digits
Character classes
Want to match a collection of characters without predefined metacharacters
You can easily specify a character range
Something like [0-9] represents exactly the same meaning as \d: a number
In the same way, [a-z0-9a-z_] is exactly the same as \w
Branch conditions
The branch conditions in the regular expression refers to several rules, if meet with any rules should be as a match, the specific method is to use | separates different rules
0 \ d {2} – \ d {8} | 0 \ d {3} – \ d {7} this expression can match the phone number of the two separated with: one is the three codes, eight local number (e.g., 010-12345678), one kind is four area code, seven local (0376-2233445) no.
When using branching conditions, pay attention to the order in which the conditions are placed
grouping
To repeat multiple characters, use parentheses to specify subexpression (also called grouping)
(\ d {1, 3}. {3}\d{1,3} is a simple expression for matching IP addresses
\d{1,3} matches 1 to 3 digits
(\ d {1, 3}. {3} matches three digits with an English period (the whole is the group) repeated 3 times
Finally add a one – or three-digit number (\d{1,3})