Regular Expressions 30-minute introductory tutorial
Three common points of knowledge:
1, lazy matching: the regular engine is greedy by default, to the minimum repetition, you need to use the lazy match character "?"
Lazy Qualifier
Code/syntax |
Description |
*? |
Repeat any number of times, but repeat as little as possible |
+? |
Repeat 1 or more times, but repeat as little as possible |
?? |
Repeat 0 or 1 times, but repeat as little as possible |
{n,m}? |
Repeat N to M times, but repeat as little as possible |
{N,}? |
Repeat more than n times, but repeat as little as possible |
Reference: Http://deerchao.net/tutorials/regex/regex.htm#greedyandlazy
2, does not capture the sexual match: On the one hand does not occupy the reverse application group number, on the other hand improves the matching efficiency
(?: EXP)//Match exp, do not capture matched text, nor assign group number to this group
Reference: Http://deerchao.net/tutorials/regex/regex.htm#backreference
3. PHP Regular expression pattern modifiers
Pattern Correction Symbols |
function Description |
I |
" Ignore case " in and regular match is case insensitive |
M |
" Multiline mode " Argument 1: If enabled, ^ and $ will match the beginning of the line to the end; if not enabled, ^ and $ will match the entire string beginning at the end; Argument 2: Treats a string as multiple lines. The default regular start "^" and End "$" Use the target note string as a single "line" character (even including newline characters). If you add "M" to the modifier, the start and end of each line that will be pointing to the string begins with "^" Ending and "$". |
S |
" single-line mode " Myth 1: Period (.) Can match any character, including line break (\ n); Myth 2: If this modifier is set, the matched string will be treated as a line, including a newline character, and the newline character will be treated as a normal string. |
X |
" Ignore whitespace " Argument 1: If enabled, whitespace characters (spaces, newline, and so on) in the regular expression are ignored, and inline annotations in the format (#注释) are ignored. In the secondary mode, you can still use \s to match whitespace characters. Myth 2: Ignore whitespace, unless escaping is not ignored. |
E |
used only in the Preg_replace () function, Replace the reverse reference in the replacement string with a normal substitution, evaluate it as a PHP code, and replace the searched string with its result. Note: This parameter is not recommended, related articles are as follows: php function preg_replace () regular replace all strings that match the criteria Replace Preg_replace with Preg_replace_callback |
A |
If you use this modifier, the expression must be the beginning of the matching string. For example, "/a/a" matches "ABCD". |
D |
The $ character Fu Quan in the pattern matches the end of the target character. Without this option, if the last character is a line break, the dollar sign will also match this character. If modifier m is set, this is ignored. |
E |
In contrast to "M", if this modifier is used, "$" will match the end of the absolute string instead of the newline character, which is turned on by default. |
U |
Greedy mode, similar to the role of question marks, the maximum match is greedy mode.
|
the common metacharacters
Code |
Description |
. |
Match any character except newline (\r\n,\r,\n) |
\w |
Match letters or numbers or underscores or kanji |
\s |
Matches any whitespace character , including spaces , tabs (tab), line breaks , Chinese full-width spaces , and so on |
\d |
Match numbers |
\b |
Match the beginning or end of a word |
^ |
Match the start of a string |
$ |
Match the end of a string
|
PHP Regular Expression 30-minute introductory tutorial