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

WBOY
Release: 2016-06-13 13:47:44
Original
894 people have browsed it

正则里的贪婪表达式。。

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>
Copy after login


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

谢谢!

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

即使去掉了$ 也只能匹配第一个aaa,因为你加了 ^
Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!