Regular expressions and PCRE functions in PHP. In PHP, the regular expression and PCRE function PCREPHP use different regular expressions: PCRE (Perl-compatible notation, preg _ *) function and POSIX (POSIX extension notation, regular expressions and PCRE functions in PHP
PCRE
PHP uses two different regular expressions: PCRE (Perl-compatible notation, preg _ *) function and POSIX (POSIX extension notation, ereg _ *) function. Fortunately, POSIX family functions have been discarded since PHP 5.3.0.
Regular expression identifier
Frequently used delimiters are forward slashes (/), hash symbols (#), and reverse symbols (~). The following examples use valid delimiters.
/foo bar/#^[^0-9]$#+php+%[a-zA-Z0-9_-]%{this is a pattern}
You can add a pattern modifier to the end separator.
Metacharacters
Some characters are given special meanings, so that they do not simply represent themselves. this encoding character with special meanings in the pattern is calledMetacharacters
.
Metacharacters |
Description |
|
It is generally used to escape characters. |
^ |
The start position of the assertion target (or the first row in multi-row mode) |
$ |
The end position of the assertion target (or the end of the row in multi-row mode) |
. |
Match any character except linefeed (default) |
[ |
Start character class definition |
] |
End character class definition |
| |
Start an optional branch |
( |
Start tag of the sub-group |
) |
End tag of the sub-group |
? |
As a quantizer, it indicates 0 or 1 match. It is behind the quantifiers to change the greedy nature of quantifiers. (Query quantifiers) |
* |
Quantifiers, 0 or multiple matches |
+ |
Quantifiers, matching once or multiple times |
{ |
Start marking of custom quantifiers |
} |
Custom quantifiers end mark |
The section in the square brackets of the pattern is called "character class ". Only the following metacharacters are available in a character class.
Metacharacters |
Description |
|
Escape characters |
^ |
If it is used only as the first character (inside square brackets), it indicates that the character class is reversed. |
- |
Mark character range |
Character class
The content in square brackets is the character class.
There are some pre-defined character classes
Character class |
Description |
D |
Any decimal number |
D |
Any non-decimal number |
H |
Any horizontal white space character (since PHP 5.2.4) |
H |
Any non-horizontal white space character (since PHP 5.2.4) |
S |
Any blank character |
S |
Any non-blank characters |
|
Any vertical blank character (since PHP 5.2.4) |
V |
Any non-vertical white space character (since PHP 5.2.4) |
W |
Any word character |
W |
Any non-word character |
Atom
Visible atom
For exampleabc
Invisible Atom
For example
Quantifiers
Quantifiers |
|
* |
Equivalent to {0 ,} |
+ |
Equivalent to {1 ,} |
? |
Equivalent to {0, 1} |
Assertions
Simple asserted codes include, B, A, Z, z, ^, and $
Forward-looking assertions
Test from current position forward
(?=)
(?!)
w+(?=;)
Match a word followed by a semicolon, but the matching result does not contain a semicolon
Houzhan assertions
Test backward from current position
(?<=)
(?
(? Used to find any bar that is not "foo"
Pattern modifier
Pattern modifier
|
|
U
|
This modifier reverses the "greedy" pattern of quantifiers, making them non-greedy by default.
|
i
|
Case insensitive match
|
x
|
Ignore blank
|
s
|
The dot metacharacters match all characters, including line breaks. If this modifier does not exist, the dot does not match the linefeed.
|
…
|
|
PCRE function
Preg_filter-execute a regular expression to search for and replace preg_grep-return the array entry preg_last_error in matching mode-return the error code preg_match_all generated by the last PCRE regular expression execution-execute a global regular expression to match preg_match-execute a regular expression matches preg_quote-escape regular expression character preg_replace_callback_array-Perform a regular expression search and replace using callbackspreg_replace_callback-executes a regular expression search and replaces preg_replace with a callback to execute a regular expression search and replace preg_split-use a regular expression to separate strings
Pcre php uses two different regular expressions: PCRE (Perl-compatible notation, preg _ *) function and POSIX (POSIX extension notation ,...