php正则表达式中的非贪婪模式匹配的使用

WBOY
Release: 2016-06-23 13:29:48
Original
1261 people have browsed it

php正则表达式中的非贪婪模式匹配的使用

通常我们会这么写:

$str = "http://www.baidu/.com?url=www.sina.com/";preg_match("/http:(.*)com/", $str, $matches);print_r($matches);
Copy after login

结果:

Array ( [0] => http://www.baidu/.com?url=www.sina.com [1] => //www.baidu/.com?url=www.sina. )
Copy after login

非贪婪模式匹配:

$str = "<a http://www.baidu/.com?url=www.sina.com/";preg_match("/http:(.*?)com/", $str, $matches);print_r($matches);
Copy after login

结果:

Array ( [0] => http://www.baidu/.com [1] => //www.baidu/. )
Copy after login

简单的说只要在一个字符后面跟上限定个数的特殊字符,匹配就是非贪婪模式了。小伙伴们是否理解了呢? 

版权声明:本文为博主原创文章,未经博主允许不得转载。

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template