Regular expression functions in PHP

  • 2020-05-12 02:23:20
  • OfStack

Regular expressions (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 grammar:
1. The delimiter
Must appear in pairs and can use any character other than 0-9a-zA-Z \
2. Atoms
1. The visible and invisible characters that a regex needs to match are atoms
2.1 a regular expression contains at least one atom
3. Escape with "\" backslash when matching semantic symbols such as "(", "[", "^", etc

Atomic characters:
\f match page break character
\n matches newline characters
\r matching carriage return
\t matches tabs
\v matches vertical tabs

3. Metacharacters
\ escape character
^ matches the beginning of the string
$matches the end of the string
.matches any single character other than "\n"

* matches the previous subexpression 0 or more times
+ matches the previous subexpression 1 or more times
? Matches the previous subexpression 0 or 1 times

{n} matches n times
{n,} match n times or n times or more
{n,m} matches n at least and m at most (n) < =m)

The brackets in [] represent the table of atoms, and the atoms in the middle are all equal. When matching, matches any 1 character in the table
[^] iambic, excluding the characters contained in the following table of atoms.

(pattern) matches pattern and gets this 1 match.
\num to the retrieved num matched reference.


(? :pattern) matches pattern but does not get this 1 match

(? =pattern) positive positive check, not get a match, for example :windows(? =XP|7) can match windows in windowsXP cannot match windows in windows98
(? ! =pattern) positive negation to find a match, e.g. windows(? ! 98|2000), can match windows in windowsXP, cannot match windows in windows98
(? < =pattern) reverse positive check, not get a match. For example: (? < =My|Postgre)SQL can match SQL in MySQL, but cannot match SQL in MSSQL
(? < ! pattern) reverse negative precheck, not get match. For example: (? < ! My|Postgre)SQL matches SQL in MSSQL, but not SQL in MySQL

\b matches word boundaries
\B matches characters other than word boundaries

\d matches any 1 digit. Equivalent to [0-9]
\D matches any 1 character other than a number. Equivalent to [^ 0-9]

\s matches any 1 blank character (including space, TAB, page break, etc.). Equivalent to [\ f \ n \ r \ t \ v]
\S matches any 1 non-white space character. Equivalent to [^ \ f \ n \ r \ t \ v]

\w matches any number, letter or underscore. Equivalent to [0-9 a zA - Z]
\W matches any character that is not a number, letter, or underscore. Equivalent to [^ 0-9 a zA - Z]

4. Schema modifier
i is not case sensitive
m if there is a carriage return or line feed in this pattern,^ and $match the beginning and end of each line
s enables. To match \n
x ignores white space
U abolish greed, equivalent to (.*?)
A like ^ effect 1
D does not ignore the carriage return at the end. When there is a $at the end, put the carriage return after the matching string and the $will still match it successfully. But when you add D, the return at the end, no longer matches

NOTE: regular expressions are matched from left to right


Related functions:
preg_filter - performs 1 regular expression search and replace
preg_grep - returns an array entry for the matching pattern
preg_last_error - returns the error code generated by the last PCRE regular execution
preg_match_all - performs a global regular expression match
preg_match - performs 1 regular expression match
preg_quote - escapes the regular expression character
preg_replace_callback - performs 1 regular expression search and replaces it with 1 callback
preg_replace - performs the search and replace of a regular expression
preg_split - delimits the string with a regular expression

Related articles: