One.the composition of regular expressions1. Separators can be any character except letters, numbers, backslashes and blanks, such as/,!,#,%,|,~, etc; usually/,!, ~2. Expression: consists of some special characters and non-special characters. 3. Modifier: Used to turn a function on or offMode two.Meta characters are commonly used in the following types of meta characters.match any characters except newline character \w match letters or numbers or underscores or Kanji \s match any white space character \d match a number \b match a word start or end^Match string start $ match string end-represents a range [] matches any one of the characters in parentheses* + ?quantifier three.character groups if matching does not have a pre-defined metacharacters,You can use character groups, just list them in [], and [Aoeiou] matches any one of the English vowels; [.?!] Match punctuation. ? !PS: Although there are many characters in the [] number,but he only matches a single word Fu Shi.escaped if you want to match the meta character itself, such as, a direct match, will be treated as a meta-character,This time to use escape; \?as:\? can be matched in string containing?the string PS:Metacharacters in character groups do not need to be escaped five.Antisense \w matches except for letters or numbers or underscores or Kanji \s match except any white space character \d match except for a number \b match is not the word start or end of the position character group is added^[^ABC] matches any character PS other than ABC: The characteristic of antisense is the opposite of some known metacharacters and is capitalized.Character Group exception six.Branch|indicates that there are multiple possible matches for a branch branch, such as to match a cat, Hat,fat,toat,You can use the branching form (c|h|f|To ) ATPs: When a branch condition is matched, each condition is tested from left to right, and other conditions are no longer considered if a branch is satisfied.Seven.grouping repeats a single character requires a qualifier to do, repeating multiple characters requires grouping, grouping with parentheses to specify an expression, and then specifying the number of times the expression repeats. 1.Capture (Exp) Match exp,and capture the text into the auto-named group (?<name>Exp) Match exp,and capture the text into the name group (?:Exp) match exp, do not capture,also does not assign the group number example 1: <?PHP$str= ' Cat ';Preg_match_all('/(c). * * ',$str,$arr);Var_dump($arr); output (auto-named group):Array(2) { [0]=>Array(1) { [0]=>string(3) "Cat" } [1]=>Array(1) { [0]=>string(1) "C"}} Example 2: <?PHP$str= ' Cat ';Preg_match_all('/(? <name>c). */',$str,$arr);Var_dump($arr) output (more than one test group):Array(3) { [0]=>Array(1) { [0]=>string(3) "Cat" } ["Test"]=>Array(1) { [0]=>string(1) "C" } [1]=>Array(1) { [0]=>string(1) "C"}} Example 3: <?PHP$str= ' Cat ';Preg_match_all('/(?: c). */',$str,$arr);Var_dump($arr); output:Array(1) { [0]=>Array(1) { [0]=>string(3) "Cat" }}2.0 Wide assertion (matches the fact,Not content) (?=Exp) matches the position of the exp front (? <=Exp) matches the position after exp (?!Exp) match the position followed by the exp (? <!Expmatches the position of the previous followed by the exp: <?PHP$str= ' Testecattest ';Preg_match_all('/(? =te)/',$str,$arr); the output:Array(1) { [0]=>Array(3) { [0]=>string(0) "" [1]=>string(0) "" [2]=>string(0) ""}} matches to three PS: By default, each grouping has a group number, the rules are left-to-right, the left parenthesis of the group is a flag, the first one appears in the group is 1, the second is 2, and so on; Grouping 0 is the entire expression.Eight.Reverse reference reverse refers to repeating the search for a previously grouped matched text.Example:$str= ' [Url]1.gif[/url] ';$img=Preg_replace('/\[url\] (\d+\.gif) \[\/url\]/', ' ',$str);Echo $img; outputmatches a group (\d+\.gif) Nine.Surround (0 wide assertion)1. The order is sure to look around (? =Exp) assert that the occurrence of the position behind can match the expression exp example: <?PHP$str= ' What is you doing testing? ';Preg_match_all('/\b\w+ (? =ing\b)/',$str,$arr);Var_dump($arr); match do and Test2. Affirmative look around in reverse order (? <=Expasserts that it appears in front of the position to match the expression exp example: <?PHP$str= ' What is you doing testing? ';Preg_match_all('/\b (? =ing)/',$str,$arr);Var_dump($arr); match doing and testing3. Order negative surround look (?!Exp)4. Reverse negative surround (<!Exp) 10. Common modifiers/Mode1.I ignore case case: <?PHP$str= ' Cat Test cat ';Preg_match_all('/cat/i ',$str,$arr);Var_dump($arr); no I modifier,Match only Cat2.m Multi-line mode only occurs in an expression^,$ at least one meta-character with line break\ n,m-Modified characters function;:$str=Cat Testcat‘; to extract Cata.No Multiline modePreg_match_all('/cat.*/',$str,$arr); b.in multiline modePreg_match_all('/cat.*$/m ',$str,$arr);3.S dot wildcard mode (.can match the line break) as: <?PHP$str=Cat TTESTCATF‘;Preg_match_all('/t.*f/s ',$str,$arr);Var_dump($arr); output:Array(1) { [0]=>Array(1) { [0]=>string(19) "T TTESTCATF" }}4.U lazy mode is equivalent to lazy match5.D end limit equivalent to end qualifier6.U enable Perl incompatible features in pcre, pattern strings are treated as Utf-8
PHP Regular Expressions Basic