Home  >  Article  >  Backend Development  >  正则里的贪婪表达式。解决思路

正则里的贪婪表达式。解决思路

WBOY
WBOYOriginal
2016-06-13 13:47:44895browse

正则里的贪婪表达式。。

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$a = '<b>aaa</b><b>bbb</b><b>ccc</b>';

$pattern1 = '/<b>.*?/';
$pattern2 = '/^<b>.*?$/';

</b></b>


用$pattern1和$pattern2与$a进行匹对,第一个能匹配出'aaa'、'bbb'、'ccc'这三个
但是用$pattern2只能匹对出'aaabbbccc',也就是.*?的贪婪模式没起作用,这是为什么???

谢谢!

------解决方案--------------------
因为你加了结尾符$ 所以它就一直匹配到字符串末尾。

即使去掉了$ 也只能匹配第一个aaa,因为你加了 ^
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