Regular expression base syntax
1.1 match different types of characters
Character class |
Matched characters |
\d |
Matches a numeric character. equivalent to [0-9]. |
\d |
Matches a non-numeric character. equivalent to [^0-9]. |
\w |
Matches any word character that includes an underscore. Equivalent to ' [a-za-z0-9_] '. |
\w |
Matches any non-word character. Equivalent to ' [^a-za-z0-9_] '. |
\s |
Matches any whitespace character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v]. |
\s |
Matches any non-whitespace character. equivalent to [^ \f\n\r\t\v]. |
. (Point number) |
Any one character |
[...] |
Any one of the characters in parentheses |
[^ ...] |
Any character in a non-parenthesis |
1.2 Positioning control characters
^ |
The subsequent pattern must be at the beginning of the string, and if it is multiple lines at the beginning of any line C # needs to set the multiline flag |
$ |
The preceding pattern must be at the end of a line, and if it is multiple lines, at the end of any line |
\a |
The preceding pattern must be at the beginning of the string, and multiline flags are ignored |
\z |
The preceding pattern must be at the end of the string, and multiline flags are ignored |
\z |
The preceding pattern must be at the end of the string or before the line break |
\b |
Matches the beginning of a word character, a word character that is one of [a-za-z0-9] |
\b |
Matches a non-word boundary position, not the beginning of a word |
1.3 Specify repeating characters
N |
matches the preceding character n times |
{N,} |
Matches the preceding character at least n times |
{N,m} |
Match the preceding characters n to M times |
? |
Match the preceding character 0 or 1 times |
+ |
Match the preceding character at least 1 times |
* |
Match the preceding character at least 0 times |
1.4 Special Control class
| |
Specifies the character substitution, where the position can be either an expression on either side of the | |
1.5 special character escape sequences
\\ |
Match "\" |
\. |
Match "." |
\* |
Match "*" |
\( |
Match "(" |
\) |
Match ")" |
\? |
Match "? ” |
\+ |
Match "+" |
\| |
Match "|" |
\{ |
Match "{" |
\} |
Match "}" |
\^ |
Match "^" |
\$ |
Match "$" |
\ n |
Match line break |
\ r |
Match carriage return |
\ t |
Match Tab key |
\v |
Match vertical Tabs |
\f |
Match a page break |
\nnn |
Matches the ASCII character specified by a three-bit octal number, such as \103 match C |
\xnn |
Matches an ASCII character specified by a two-bit 16 binary, such as \x43 match C |
\unnnn |
Matches the Unicode character specified by a 4-bit 16 binary number |
\cv |
Match a control character (such as copy CTRL + C) |
CommonRegular validation rules
1. Detecting user Names
Requirements: 5 to 17 characters are preceded by a letter, a combination of numbers and English symbols
Regular expression:/^[a-za-z][[email protected]~!?] {4,16}$/
2. Password detection
Requirements: 8 to 17 characters are combined by letters, numbers, and English symbols
Regular expression:/^[[email protected]~!?] {8,17}$/
3. Detect mobile phone number
Requirements: Accurate matching of all mobile phone numbers, including mobile, unicom, telecommunications
Regular expression:/^ (((13[4-9)) |147| ( 15[0-27-9]) |178| (18[2-47-8])) [0-9] {8}) | ((170[356]) [0-9]{7}]
| (((13[0-2)) |145| (15[56]) | (18[56]) |17[16]) [0-9]{8}) | ((170[4789]) [0-9]{7}]
| ((133|153| (18[019]) |177) [0-9]{8}) | ((170[0-2]) [0-9]{7}] $/
4. Match URL URLs
Requirements: http//And www. But there is no
For example: www.baidu.com, baidu.com, http://www.baidu.com, https://i.cnblogs.com/EditPosts.aspx?opt=1
Regular expression:/^ (((https|http|ftp|): \ /\/)? (www\.)? ([\w_\-\.] {0,61}) + (\.\w{2,10}) + (\/)? [\[email protected]&=#\-\.\/] {0,}) | (#)$/
Regular expression learning and common regular validation rules (including username, password, phone number, URL, etc.)