Home > Backend Development > PHP Tutorial > 这个匹配为什么错了,正则表达式难题,该如何处理

这个匹配为什么错了,正则表达式难题,该如何处理

WBOY
Release: 2016-06-13 13:40:24
Original
884 people have browsed it

这个匹配为什么错了,,正则表达式难题

$txt="[face='黑体']12345[/face][face='黑体']哈哈[/face]";
$return=preg_match_all("/\[face='(.*)'](.+?)\[\/face]/",$txt,$tar);
echo "

";<br>	print_r($tar);<br>?><br><br>我的匹配的意思是以[face='任意数']  以[/face]结尾的最短字符串。。。<br><br>哪里错了???<br><br>任意数这里好像似乎应该排除[/face]<br><br>Array<br>(<br>     [0] => Array<br>         (<br>             [0] => [face='黑体']12345[/face][face='黑体']哈哈[/face]<br>         )<br><br>     [1] => Array<br>         (<br>             [0] => 黑体']12345[/face][face='黑体<br>         )<br><br>     [2] => Array<br>         (<br>             [0] => 哈哈<br>         )<br><br>)<br><br>结果错了,,匹配的最长的字符串,晕<br><br><font color="#e78608">------解决方案--------------------</font><br>
……/\[face='(.*)']……   =>  ……/\[face='(.*?)']……<br><br>没有?,就是贪婪,结果如你所贴
<br><font color="#e78608">------解决方案--------------------</font><br>/\[face='(.*)'](.+?)\[\/face]/U   加个U也可以。 <div class="clear">
                 
              
              
        
            </div>
Copy after login
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