Regular expressions and pcre functions in PHP
PCRE
PHP has two different ways to use regular expressions: PCRE (Perl-compatible notation, preg_*) functions, and POSIX (POSIX extended notation, ereg_*) functions. Fortunately, the POSIX family function was deprecated from PHP 5.3.0.
Regular expressions
delimiting characters
Frequently used delimiters are forward slashes (/), hash symbols (#), and inverse symbols (~). The following example is a pattern that uses a valid delimiter
/foo bar/#^[^0-9]$#+php+%[a-zA-Z0-9_-]%{this is a pattern}
You can add a pattern modifier after the end delimiter
Metacharacters
Some characters are given special meanings, so that they are no longer purely representative of themselves, the pattern of this special meaning of the encoded character is called 元字符 .
| Meta character |
Description |
|
Generally used to escape characters |
| ^ |
Assert the starting position of the target (or the beginning of a line in multiline mode) |
| $ |
Assert the end position of the target (or end of line in multi-line mode) |
| . |
Match any character except line break (default) |
| [ |
Start character class definition |
| ] |
End character class definition |
| | |
Start an optional branch |
| ( |
The start tag of the child group |
| ) |
The end tag of the child group |
| ? |
As a quantifier, represents 0 or 1 matches. The greedy character that is used to change quantifiers after quantifiers. (check quantifier) |
| * |
quantifier, 0 or more matches |
| + |
quantifier, 1 or more matches |
| { |
Custom quantifier start tag |
| } |
Custom quantifier end Tag |
The part of the pattern in brackets is called the "character class". There are only the following available metacharacters in a character class
| Meta character |
Description |
|
Escape character |
| ^ |
Indicates that the character class is reversed only when it is the first character (in square brackets) |
| - |
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 |
arbitrary decimal digits |
| D |
Any non-decimal number |
| H |
Any horizontal whitespace character (since PHP 5.2.4) |
| H |
Any non-horizontal whitespace character (since PHP 5.2.4) |
| S |
Any whitespace character |
| S |
Any non-whitespace character |
|
Any vertical whitespace character (since PHP 5.2.4) |
| V |
Any non-vertical whitespace character (since PHP 5.2.4) |
| W |
Any word character |
| W |
Any non-word character |
Atomic
Visible atoms
Such asabc
Non-visible atoms
Such as
Quantifiers
| quantifier |
|
| * |
Equivalent to {0,} |
| + |
Equivalent to {1,} |
| ? |
Equivalent to {0,1} |
Assertion
The simple assertion code has, B, A, Z, z, ^, $
Forward-looking assertions
Test forward from current position
(?=)(?!)
w+(?=;)Match a word followed by a semicolon but the match result does not contain a semicolon
Post-Juniper Assertions
Test backwards from the current location
(?<=)(?
(? 用于查找任何前面不是 ”foo” 的 ”bar”
模式修饰符
模式修饰符 |
|
U |
这个修饰符逆转了量词的”贪婪”模式,使量词默认为非贪婪的 |
i |
大小写不敏感匹配 |
x |
忽略空白 |
s |
点号元字符匹配所有字符,包含换行符。如果没有这个修饰符,点号不匹配换行符 |
… |
|
PCRE 函数
preg_filter — 执行一个正则表达式搜索和替换preg_grep — 返回匹配模式的数组条目preg_last_error — 返回最后一个PCRE正则执行产生的错误代码preg_match_all — 执行一个全局正则表达式匹配preg_match — 执行一个正则表达式匹配preg_quote — 转义正则表达式字符preg_replace_callback_array — Perform a regular expression search and replace using callbackspreg_replace_callback — 执行一个正则表达式搜索并且使用一个回调进行替换preg_replace — 执行一个正则表达式的搜索和替换preg_split — 通过一个正则表达式分隔字符串
http://www.bkjia.com/PHPjc/1050841.html www.bkjia.com true http://www.bkjia.com/PHPjc/1050841.html techarticle Regular Expressions and PCRE functions in PHP PCRE PHP has two different ways to use regular expressions: PCRE (Perl-compatible notation, preg_*) functions and POSIX (POSIX extended notation, ...) .