Home > php教程 > php手册 > 浅谈php正则表达式中的非贪婪模式匹配的使用

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

WBOY
Release: 2016-06-06 20:16:43
Original
1720 people have browsed it

这里通过实例简单介绍了下php正则表达式中的非贪婪模式匹配的使用方法,有需要的小伙伴们参考下吧

通常我们会这么写:

复制代码 代码如下:


$str = "";
preg_match("/http:(.*)com/", $str, $matches);
print_r($matches);

结果:

复制代码 代码如下:


Array ( [0] => [1] => //www.baidu/.com?url=www.sina. )

非贪婪模式匹配:

复制代码 代码如下:


$str = "";
preg_match("/http:(.*?)com/", $str, $matches);
print_r($matches);

结果:

复制代码 代码如下:


Array ( [0] => [1] => //www.baidu/. )

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

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template