Home  >  Article  >  Backend Development  >  A brief discussion on the use of non-greedy pattern matching in PHP regular expressions, a brief discussion on regular expressions_PHP tutorial

A brief discussion on the use of non-greedy pattern matching in PHP regular expressions, a brief discussion on regular expressions_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:13:10957browse

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?

www.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...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn