Php regular expression failure-maximum backtracking (pcre. backtrack_limit)/recursive restriction

Source: Internet
Author: User
Tags php regular expression
Php regular expression failure-maximum backtracking (pcre. backtrack_limit)/recursive restrictions yesterday, a colleague was processing a regular expression collection program and found a strange phenomenon: the local regular expression rules can be matched by any write, however, after uploading to the server, we found that no regular expression can be matched. I adjusted it for half a day, but I gave up? Then ............, Unwilling ......, After dinner, open your computer and blog casually, or g php regular expression failure-maximum backtracking (pcre. backtrack_limit)/recursive restriction

Yesterday, my colleague was processing a regular expression collection program and found a strange phenomenon: the local regular expression rules can be matched by random writing, but after uploading to the server, I found that no regular expression can be matched. I adjusted it for half a day, but I gave up? Then ............, Unwilling ......, After dinner, I opened my computer and casually went crazy about my blog. I went to google and talked about the regular expression rules. Finally, I calmed down and analyzed it.

My colleagues and I installed wamp on their computers. The php version was 5.3.x and the php version on the server was 5.2.x. Is this a problem and I am skeptical about it, I opened the manual text processing-pcre function-preg_match_all. good guy, there was a function preg_last_error on it. open it and try my code with it. the returned error is PREG_BACKTRACK_LIMIT_ERROR, it turned out to be an environment variable problem, so I added ini_set ("pcre. backtrack_limit ", 1000000); the results are captured normally. I looked back at the following manual and found the following introduction:


Finally, I checked some information on the internet. one of the bloggers wrote something very fine: Let's turn to some related content and explain it:

Two configuration items are provided in the pcre extension of PHP.


1 pcre. backtrack_limit // maximum number of backtraces
2 pcre. recursion_limit // maximum number of embedded sets


The default backtarck_limit value is 100000 (0.1 million ).
Now, the key to understanding the cause of this problem is "backtracking ".
This regular expression uses the non-greedy mode. in simple terms, the non-greedy mode does not match the regular expression. record the alternative status, and assign the matching control to the next matching character of the regular expression. when the subsequent matching fails, repeat and perform matching.


For example:
SOURCE string: aaab regular :.*?
When the matching process starts, ". *?" First, obtain the control of matching. because the mode is not greedy, the matching control is given priority and the matching control is given to the next matching character "B ", "B" fails to match at the source string position 1 ("a"), so it traces back and returns the matching control ". *? ", At this time, ". *?" Match a character "a" and give control to "B" again. after this repetition, the matching result is obtained. a total of three backtracing occurs in this process.


Finally, let's talk about it.

1. after PHP 5.2, it provides: int preg_last_error (void) Returns the error code of the last PCRE regex execution.
We should always check the return value of this function. when it is not zero, it indicates that the previous regular function has an error. in particular, for the example in the article, an error is returned (PREG_BACKTRACK_LIMIT_ERROR)
2. if the non-greedy mode leads to too much backtracking, there will inevitably be some performance problems. writing regular expressions appropriately can avoid this problem. especially when processing large amounts of text data, if the regular expression design is careless, it is easy to cause deep nesting. considering the performance, we recommend that you use string processing instead of string processing as much as possible.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.