String $ str '& lt; script & gt; 123456 & lt;/script & gt;'; regular expression $ strRegex1 '% & lt; script & gt ;. + & lt; \/script & gt; % '; $ strRegex2' % & lt; script & gt ;. +? & Lt; \/script & gt; % ';
String
$str = '《script》123456《script》';
The regular expression is
$strRegex1 = '%《script》.+<\/script>%';$strRegex2 = '%《script》.+?<\/script>%';$strRegex3 = '%《script》(?:(?!<\/script>).)+<\/script>%';
How many backtracing operations will be performed for these three regular expressions ??
Answer:
$ StrRegex1 = '% script. + <\/script> %'; // 9 times. remember to differentiate escape characters. $ StrRegex2 = '% script. +? <\/Script> % '; // five times $ strRegex3 =' % script (? :(?! <\/Script>).) + <\/script> % '; // 7 times
For the matching rule of the first greedy match, the 9 backtracking times constitute the backtracking and backtracking times when the regular [] matches the string "", exactly the length of the string.
The second type of non-greedy matching rule, which is a regular [. + ?] Backtracing formed when the string "123456" is matched. The number of backtracking times, which is the string length minus the minimum number of times. That is, 6-1 = 5 times. If the regular expression is 【.*?] The number of backtracking times is 6.
The third regular expression is a zero-width assertion, or a loop view. (Not to mention .)
In the NFA regular expression engine, backtracking is his soul. therefore, whether it is greedy, non-greedy, or loop-based writing, backtracking will certainly occur, this cannot be avoided (the words are not accurate). However, we can reduce the number of backtracing times or protect some matching rules from backtracking.
As laruence mentioned in the previous BLOG talked about a large number of backtracking problems caused by non-greed, we can know that backtracking is indeed the culprit of resource waste. can we not let it be traced back?
The answer is yes. in the NFA engine, there is a concept called curing group. Reference concepts in the book
Specifically, use "(?> ...)」 There is no difference between the matching and normal matching, but if the matching is performed after this structure (that is, after the brackets are closed), all the standby states in this struct will be abandoned. That is to say, at the end of the fixed group match, the matched text has been solidified into a unit and can only be retained or abandoned as a whole. The standby states that have not been tried in the subexpressions in brackets no longer exist, so backtracking cannot always select the status (at least, when this structure is matched, locked in ).
So what is the purpose of curing groups? Let's give an example. (I can't find a suitable example, so I have to borrow the example from the book)
For example, to process a batch of data, the original format is 123.456. Later, due to the floating point display problem, some data formats are changed to 123.456000000789. Therefore, only two or three digits after the decimal point must be retained. However, the last digit cannot be 0. how can I write this regular expression? (The number after the decimal point is directly considered below). after writing the regular expression, we need to use this regular expression to match the data and replace the original data with the matching result.
First, we can immediately write such a regular [\. \ d [1-9]? \ D *], PHP code is
$ Str = preg_replace ('\. (\ d [1-9]?) \ D * ',' \ 1', $ str );? // Reverse reference of group1 in matching results
Obviously, this method is used to process some data in the format of 123.456. to improve efficiency, we need to process this regular expression. From the comparison between the string 123.456 and others, we found that there was no number after the data in question 123.456, so we processed it in vain. Well, let's rebuild this regular expression and change the quantizer * behind it to +. in this way, the 1 or 2 digits after the decimal point of 123.45 won't be processed in vain, if more than three digits exist, the processing is normal. Its PHP code is
$str = preg_replace('\.(\d\d[1-9]?)\d+','\\1',$str);
Okay. is this regular expression correct ?? Are you sure you want? In the previous blog, we learned about the matching principle. then, let's analyze the matching process of this regular expression.
String "123.456", the regular expression is [\. (\ d [1-9]?) \ D +]. let's take a look.
First (no more than 123 decimal points ),【\.] match ". ", matching successful. give control to the next [\ d], [\ d] match" 4 ", and give control to the second [\ d ], this [\ d] matches "5" successfully, and then gives control to [1-9]? ], Because the quantifiers are 【?], The regular expression follows "Quantifiers first match", and here is 【?], A backtracking point will be left. Then match "6", and then give the control to [\ d +]. [\ d +] it is found that there are no characters behind it. The rule of "back-in-first-out" is followed most, return to the previous backtracking point and perform a match. then, [1-9]?] The matched character "6" will be returned. [1-9]?] Matching "6" is successful. The matching is complete. [(\ D [1-9]?)] The matching result is indeed "45", not the "456" we want, and "6" is matched by [\ d +. So what should we do? Can I set [1-9]? If a match is successful, do not backtrack it? This uses the "curing group" mentioned above. the regular engine used in PHP (preg_replace function) supports curing group. we can change the code to the following method based on the compiling of curing group:
$str = preg_replace('\.(\d\d(?>[1-9]?))\d+','\\1',$str);
In this case, the "123.456" character string does not meet the requirements and will not be matched. Then we can fulfill our requirements.
In the above example, we know the role of the solidified grouping. can we modify the non-greedy backtracking problem written on the BLOG of laruence so that it does not backtrack?
Let's take a look at the answer from laruence.
/《script》[^<]*<\/script>/is
Laruence writes a lot. All characters except "<" are excluded, and the middle part does not backtrack, which is highly efficient. However, if there is a character "<" in the middle (the following code)
《script》if a < b《script》
This regular expression of laruence cannot be matched, so we cannot implement the functions we want.
Then we can implement this requirement based on the fixed grouping and loop View (zero width assertion). Finally, we will provide the following regular expressions and PHP code examples:
$ Reg = '% script (?> [^ <] *) (?> (?!
) <[^ <] *) * Script % is '; $ str = str_pad ("script", 111111 ,"*"); // the length of the character is greater than the PHP backtracking limit of 100000 $ str. = 'If a <B; if B> c; script '; // add a few test characters including <> $ ret = preg_replace ($ reg, "OK ", $ str); print_r ($ ret); // print the result OK to verify that the matching is correct var_dump (preg_last_error (); // The Last matching error. The output is int (0)