A brief discussion on the use of non-greedy pattern matching in PHP regular expressions, a brief discussion on regular expressions
Usually we would write like this:
Copy code The code is as follows:
$str = "http://www.baidu/.com?url=www.sina.com/";
preg_match("/http:(.*)com/", $str, $matches);
print_r($matches);
Result:
Copy code The code is as follows:
Array ( [0] => http://www.baidu/.com?url=www.sina.com [1] => //www.baidu/.com?url=www.sina. )
Non-greedy pattern matching:
Copy code The code is as follows:
$str = "http://www.baidu/.com?url=www.sina.com/";
preg_match("/http:(.*?)com/", $str, $matches);
print_r($matches);
Result:
Copy code The code is as follows:
Array ( [0] => http://www.baidu/.com [1] => //www.baidu/. )
Simply put, as long as a character is followed by a limited number of special characters, the matching is a non-greedy mode. Do you guys understand?
http://www.bkjia.com/PHPjc/917037.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/917037.htmlTechArticleA brief discussion on the use of non-greedy pattern matching in PHP regular expressions. A brief discussion on regular expressions. Usually we do this Write: Copy the code as follows: $str = "http://www.baidu/.comurl=www.sin...