Perl Regular Expression template engine regular expression debugging tips

Source: Internet
Author: User
Tags perl regular expression
The template engine based on regular expression substitution can easily meet the limit of the maximum backtracking/recursion of regular expressions.
Lazy matching is not scary, normally the template is not enough, often does not exceed the limit, the Discuz template engine is heavily used. But instead of paying attention and not learning, it is easy to write mistakes and encounter problems.
When preg_* returns NULL, it is important to note that the judgment function is is_null.
Mistakes are not scary, but it's a good idea to have the output of the error intact, so debugging is easy.
In addition to the output error reason, but also output matching text and the use of the regular, so it is easy to debug.
PHP code

Copy the Code code as follows:


if (Is_null ($tmp)) {
$error _code = Preg_last_error ();
Switch ($error _code) {
Case PREG_NO_ERROR:
Echo ' Preg_no_error ';
Break
Case PREG_INTERNAL_ERROR:
Echo ' Preg_internal_error ';
Break
Case PREG_BACKTRACK_LIMIT_ERROR:
Echo ' Preg_backtrack_limit_error ';
Break
Case PREG_RECURSION_LIMIT_ERROR:
Echo ' Preg_recursion_limit_error ';
Break
Case PREG_BAD_UTF8_ERROR:
Echo ' Preg_bad_utf8_error ';
Break
Case PREG_BAD_UTF8_OFFSET_ERROR:
Echo ' Preg_bad_utf8_offset_error ';
Break
Default
Echo ' Unknow ERROR ';
}
Exit
}


Resources
1, laruence, "depth of the regular (PCRE) maximum backtracking/recursion limit"
2, PHP Chinese manual preg_last_error

The above describes the Perl regular Expression template engine regular expression debugging tips, including the Perl regular expression aspects of the content, I hope to be interested in PHP tutorial friends helpful.

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