Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Example Analysis of regular expression efficiency greed, non-greed and backtracking in PHP

Shulou Source: shulou.com Published: 2022-06-02 04:58:13 10月03日 Update

This article will explain in detail the example analysis of regular expression efficiency greed, non-greed and backtracking in PHP. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

If a student wants to filter the content between them, that's how he writes the rules and procedures.

$str = preg_replace ('%. +?% iPrecious and not greedy).

It seems that there is no problem, but in fact it is not. If

$str = 'alert ([xss_clean])'

Then after the above program processing, the result is

$str = 'alert ([xss_clean])'; $str = preg_replace ('%. +?); / / non-greedy print_r ($str); / / $str output to alert ([xss_clean])

Still can't get the effect he wants. The above is not greedy, but also called inertia. Its non-greedy logo is followed by a quantifier metacharacter, such as +, *,? (quite special, I will write about it in the future BLOG), etc. That is, the logo is not greedy, if not written? Is greed. such as

$str = 'alert ([xss_clean])'; $str = preg_replace ('%. +% iGray print_r ($str); / / $str output is

Tags: Greed character regular expression string result success control control that is face example again classmate engine program article quantifier efficiency example Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Apple MariaDB Docker macOS