PHP regular Failure-Max backtracking (pcre.backtrack_limit)/recursion limit

Source: Internet
Author: User
PHP regular Failure-Max backtracking (pcre.backtrack_limit)/recursive limit

Yesterday, colleagues in the processing of a regular collection program, found a strange phenomenon: in the local regular rules can be matched to any write, but uploaded to the server after the discovery of the regular how to write is not matched. Adjusted for a half day without effect, give up? Then ..., not reconciled ..., after dinner, open the computer random crazy blog, or Google a bit, about the regular rules, find to find are talking about how to write regular expressions and so on, and so on, and so on. Finally the heart to analyze a bit.

I and colleagues of the computer installed are wamp,php version are 5.3.x, server PHP version is 5.2.x, is this problem, holding the mood of doubt, opened the manual text processing-pcre function-preg_match_all, good guy originally it has a function preg_ Last_error, open to see, then use him to try my code, the result of the return error is preg_backtrack_limit_error, the original is the environment variable problem, so at the beginning of my code to add Ini_set ("Pcre.backtrack_ Limit ", 1000000); The result is a normal crawl. Look back at the next manual, found the following description:


Finally on the Internet to check some information, one of the bloggers wrote a very fine: below to turn some relevant content, explain:

In PHP's pcre extension, two settings are provided.


1 Pcre.backtrack_limit//maximum number of backtracking
2 Pcre.recursion_limit//maximum number of embedded arrays


The default backtarck_limit is 100000 (100,000).
The key to figuring out the cause of the problem now is what is "backtracking".
This regular, the use of non-greedy mode, non-greedy pattern matching principle is simple, in the case can not be matched, priority mismatch. Records the alternate state and gives the match control to the next matching character of the regular expression, followed by a match when the subsequent match fails.


As an example:
SOURCE string: Aaab Regular:. *?
When the matching process starts, ". *?" First get the matching control, because the non-greedy mode, so the first mismatch, the matching control to the next match character "B", "B" in the source string position 1 match failed ("a"), so backtracking, the matching control is returned to ". *?", this time, ". *?" Match a character "a", and again give control to "B", so repeatedly, finally get the matching result, this process has taken place 3 times backtracking.


Finally, I'd like to say a little bit.

1, after PHP 5.2, provided: 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 to indicate an error in the previous regular function, especially for the example of the article, error return (PREG_BACKTRACK_LIMIT_ERROR)
2, non-greedy mode caused too much backtracking, there will be some performance problems, appropriate to write down the regular, it is possible to avoid this problem. Especially in the text processing of large amounts of data, if the regular design inadvertently, it is easy to lead to deep nesting, in addition to the performance, it is recommended to use string processing as far as possible instead of string processing.


  • 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.