Home > Backend Development > PHP Tutorial > PHP regular expression efficiency greedy, non-greedy and backtracking analysis (recommended)

PHP regular expression efficiency greedy, non-greedy and backtracking analysis (recommended)

高洛峰
Release: 2023-03-04 17:16:02
Original
1884 people have browsed it

Let’s first understand what is greedy in regular expressions and what is non-greedy? Or what is matching priority quantifier and what is ignoring priority quantifier?

Okay, I don’t know what the concept is, let’s give an example.

A student wanted to filter the content between them. This is how he wrote the regular rules and procedures.

$str = preg_replace(&#39;%<script>.+?</script>%i&#39;,&#39;&#39;,$str);//非贪婪
Copy after login

It seems like there is nothing wrong with it, but in fact it is not. If

$str = &#39;<script<script>alert(document.cookie)</script>>alert(document.cookie)</script>&#39;;
Copy after login

then after the above program processing, the result is

$str = &#39;<script<script>alert(document.cookie)</script>>alert(document.cookie)</script>&#39;;
$str = preg_replace(&#39;%<script>.+?</script>%i&#39;,&#39;&#39;,$str);//非贪婪
print_r($str);
//$str 输出为 <script>alert(document.cookie)</script>
Copy after login

Still He couldn't achieve the effect he wanted. The above is non-greed, and some are called laziness. The sign of non-greedy is to add ? after the quantitative metacharacter, such as +?, *?, ?? (more special, I will write about it in the future BLOG), etc. That is, it means non-greedy. If you don’t write ?, it means greedy. For example

$str = &#39;<script<script>alert(document.cookie)</script>>alert(document.cookie)</script>&#39;;
$str = preg_replace('%<script>.+</script>%i','',$str);//非贪婪
print_r($str);
//$str 输出为 
            
            

            
        
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template