Here, we will briefly introduce the usage of non-greedy pattern matching in php regular expressions through examples. if you need it, let's take a look at it. we usually write it like this:
The code is as follows:
$ Str = "http://www.baidu/.com? Url = www.sina.com /";
Preg_match ("/http :(. *) com/", $ str, $ matches );
Print_r ($ matches );
Result:
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:
The code is as follows:
$ Str = "http://www.baidu/.com? Url = www.sina.com /";
Preg_match ("/http :(.*?) Com/", $ str, $ matches );
Print_r ($ matches );
Result:
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 specified number of special characters, matching is a non-greedy mode. Have you understood this?