PHP100 2019-03-28
通常我们会这么写:
代码如下:
$str = "<a href="http://www.baidu/.com?url=www.sina.com/">http://www.baidu/.com?url=www.sina.com/</a>"; preg_match("/http:(.*)com/", $str, $matches); print_r($matches);
结果:
代码如下:
Array ( [0] => <a href="http://www.baidu/.com?url=www.sina.com">http://www.baidu/.com?url=www.sina.com</a> [1] => //www.baidu/.com?url=www.sina. )
非贪婪模式匹配:
代码如下:
$str = "<a href="http://www.baidu/.com?url=www.sina.com/">http://www.baidu/.com?url=www.sina.com/</a>"; preg_match("/http:(.*?)com/", $str, $matches); print_r($matches);
结果:
代码如下:
Array ( [0] => <a href="http://www.baidu/.com">http://www.baidu/.com</a> [1] => //www.baidu/. )
简单的说只要在一个字符后面跟上限定个数的特殊字符,匹配就是非贪婪模式了。小伙伴们是否理解了呢?