Regular Expression (Regular Expression)
Regular Expression System:
1. POSIX
2. Perl
The regex used in PHP is PCRE:
NOTE: PCRE (Perl Compatible with Regular Expressions, Perl Compatible Regular Expressions)
PCRE Syntax:
1. delimiters
Must appear in pairs. Any character except 0-9a-zA-Z \ can be used.
2. Atom
1. the visible and invisible characters to be matched in the regular expression are both atomic.
2. A regular expression contains at least one atom
3. Escape with backslash (\) When semantic symbols such as "(", "[", and "^" need to be matched
Atomic character:
\ F match the newline
\ N match the linefeed
\ R match carriage return
\ T matching tabs
\ V matches vertical tabs
3. metacharacters
\ Escape characters
^ Match the start point of a string
$ Match the end of a string
. Match any single character except "\ n"
* Match the previous subexpression 0 or multiple times
+ Match the previous subexpression once or multiple times
? Match the previous subexpression 0 times or 1 time
{N} matches n times
{N,} matches n or more times
{N, m} matches at least n times and at most m times, (n <= m)
[] Brackets represent the atomic table, and the atomic positions in the middle are equal. When matching, match any character in the table
[^] Escape, excluding the characters contained in the atomic table.
(Pattern) matches pattern and obtains this match.
\ Num indicates the reference of the num matching obtained.
(? : Pattern) matches pattern but does not obtain this match.
(? = Pattern) Forward validation pre-check, non-get match, for example: windows (? = XP | 7) windows in windows XP cannot match windows in windows 98
(?! = Pattern) indicates a positive rejection. For example, windows (?! 98 | 2000), can match windows in windows XP, cannot match windows in windows 98
(? <= Pattern) the reverse direction must be pre-checked, not to obtain matching. Example :(? <= My | Postgre) the SQL statement matches the SQL statement in MySQL and does not match the SQL statement in MSSQL.
(? <! Pattern) in the reverse direction. Example :(? <! My | Postgre) the SQL statement can match the SQL statement in MSSQL, but cannot match the SQL statement in MySQL.
\ B match word boundary
\ B matches characters except word boundaries
\ D matches any number. Equivalent to [0-9]
\ D matches any character other than a number. Equivalent to [^ 0-9]
\ S matches any blank character (including spaces, tabs, and page breaks ). It is equivalent to [\ f \ n \ r \ t \ v]
\ S matches any non-blank character. It is equivalent to [^ \ f \ n \ r \ t \ v]
\ W matches any number, letter, or underline. It is equivalent to [0-9a-zA-Z]
\ W matches any character that is not a number, letter, or underline. It is equivalent to [^ 0-9a-zA-Z]
4. Pattern Modifier
I is case insensitive
M in this mode, if there is a carriage return or line feed, ^ and $ match the beginning and end of each row
S enables. To match \ n
X ignore Blank
U ungreedy, equivalent (.*?)
A and ^ have the same effect.
D. The carriage return is not ignored at the end. When the $ character is entered at the end, the carriage return is added after the matched string, and $ can still match the string. However, after D is added, the carriage return at the end is no longer matched.
NOTE: Regular Expressions are matched from left to right.
Related functions:
Preg_filter-execute a regular expression to search and replace
Preg_grep-return array entries in matching mode
Preg_last_error-return the error code generated by the last PCRE regular execution
Preg_match_all-execute a global regular expression to match
Preg_match-execute a regular expression to match
Preg_quote-escape Regular Expression characters
Preg_replace_callback-execute a regular expression to search and use a callback to replace
Preg_replace-search and replace a regular expression
Preg_split-use a regular expression to separate strings