我们知道在php中perl兼容正则表达式,那么对于ereg正则表达式我们需要掌握什么呢?这里我们通过perl兼容正则表达式和perl/ereg的区别分析来向你介绍ereg正则表达式,希望对你有所帮助。
虽然叫做“Perl兼容正则表达式”,但是和Perl的正则表达式相比,PHP的还是由一些不同,比如修饰符“G”在Perl里面代表全部匹配,但是在PHP中没有加入对这个修饰符的支持。
关于PHP中Ereg正则表达式的辨析:
还有就是和ereg系列函数的区别,ereg也是PHP中提供的正则表达式函数,不过和preg相比,要弱上很多。
1、ereg里面是不需要也不能使用分隔符和修饰符的,所以ereg的功能比preg要弱上不少。
立即学习“PHP免费学习笔记(深入)”;
2、关于".":点在正则里面一般是除了换行符以外的全部字符,但是在ereg里面的"."是任意字符,即包括换行符!如果在preg里面希望"."能够包括换行符,可以在修饰符中加上"s"。
3、ereg默认使用贪婪模式,并且不能修改,这个给很多替换和匹配带来麻烦。
4、速度:这个或许是很多人关心的问题,会不会preg功能强大是以速度来换取的?不用担心,preg的速度要远远比ereg快,笔者做了一个程序测试:
PHP中Ereg正则表达式time test实例:
<OL class=dp-c><LI class=alt><SPAN><SPAN><?php </SPAN></SPAN><LI><SPAN class=func>echo</SPAN><SPAN> </SPAN><SPAN class=string>"Preg_replace used time:"</SPAN><SPAN>; </SPAN><LI class=alt><SPAN class=vars>$start</SPAN><SPAN> = time(); </SPAN><LI><SPAN class=keyword>for</SPAN><SPAN>(</SPAN><SPAN class=vars>$i</SPAN><SPAN>=1;</SPAN><SPAN class=vars>$i</SPAN><SPAN><=100000;</SPAN><SPAN class=vars>$i</SPAN><SPAN>++) { </SPAN><LI class=alt><SPAN class=vars>$str</SPAN><SPAN> = </SPAN><SPAN class=string>"ssssssssssssssssssssssssssss"</SPAN><SPAN>; </SPAN><LI><SPAN>preg_replace(</SPAN><SPAN class=string>"/s/"</SPAN><SPAN>,</SPAN><SPAN class=string>""</SPAN><SPAN>,</SPAN><SPAN class=vars>$str</SPAN><SPAN>); </SPAN><LI class=alt><SPAN>} </SPAN><LI><SPAN class=vars>$ended</SPAN><SPAN> = time()-</SPAN><SPAN class=vars>$start</SPAN><SPAN>; </SPAN><LI class=alt><SPAN class=func>echo</SPAN><SPAN> </SPAN><SPAN class=vars>$ended</SPAN><SPAN>; </SPAN><LI><SPAN class=func>echo</SPAN><SPAN> " </SPAN><LI class=alt><SPAN class=func>ereg_replace</SPAN><SPAN> used time:"; </SPAN><LI><SPAN class=vars>$start</SPAN><SPAN> = time(); </SPAN><LI class=alt><SPAN class=keyword>for</SPAN><SPAN>(</SPAN><SPAN class=vars>$i</SPAN><SPAN>=1;</SPAN><SPAN class=vars>$i</SPAN><SPAN><=100000;</SPAN><SPAN class=vars>$i</SPAN><SPAN>++) { </SPAN><LI><SPAN class=vars>$str</SPAN><SPAN> = </SPAN><SPAN class=string>"ssssssssssssssssssssssssssss"</SPAN><SPAN>; </SPAN><LI class=alt><SPAN class=func>ereg_replace</SPAN><SPAN>(</SPAN><SPAN class=string>"s"</SPAN><SPAN>,</SPAN><SPAN class=string>""</SPAN><SPAN>,</SPAN><SPAN class=vars>$str</SPAN><SPAN>); </SPAN><LI><SPAN>} </SPAN><LI class=alt><SPAN class=vars>$ended</SPAN><SPAN> = time()-</SPAN><SPAN class=vars>$start</SPAN><SPAN>; </SPAN><LI><SPAN class=func>echo</SPAN><SPAN> </SPAN><SPAN class=vars>$ended</SPAN><SPAN>; </SPAN><LI class=alt><SPAN class=func>echo</SPAN><SPAN> " </SPAN><LI><SPAN class=func>str_replace</SPAN><SPAN> used time:"; </SPAN><LI class=alt><SPAN class=vars>$start</SPAN><SPAN> = time(); </SPAN><LI><SPAN class=keyword>for</SPAN><SPAN>(</SPAN><SPAN class=vars>$i</SPAN><SPAN>=1;</SPAN><SPAN class=vars>$i</SPAN><SPAN><=100000;</SPAN><SPAN class=vars>$i</SPAN><SPAN>++) { </SPAN><LI class=alt><SPAN class=vars>$str</SPAN><SPAN> = </SPAN><SPAN class=string>"sssssssssssssssssssssssssssss"</SPAN><SPAN>; </SPAN><LI><SPAN class=func>str_replace</SPAN><SPAN>(</SPAN><SPAN class=string>"s"</SPAN><SPAN>,</SPAN><SPAN class=string>""</SPAN><SPAN>,</SPAN><SPAN class=vars>$str</SPAN><SPAN>); </SPAN><LI class=alt><SPAN>} </SPAN><LI><SPAN class=vars>$ended</SPAN><SPAN> = time()-</SPAN><SPAN class=vars>$start</SPAN><SPAN>; </SPAN><LI class=alt><SPAN class=func>echo</SPAN><SPAN> </SPAN><SPAN class=vars>$ended</SPAN><SPAN>; </SPAN><LI><SPAN>?> </SPAN></LI></OL>PHP中Ereg正则表达式实例结果:
<OL class=dp-c><LI class=alt><SPAN><SPAN>Preg_replace used time:5 </SPAN></SPAN><LI><SPAN class=func>ereg_replace</SPAN><SPAN> used time:15 </SPAN><LI class=alt><SPAN class=func>str_replace</SPAN><SPAN> used time:2 </SPAN></LI></OL>
str_replace因为不需要匹配所以速度非常快,而preg_replace的速度比ereg_replace要快上不少。
PHP中Ereg正则表达式的相关内容就向你介绍到这里,希望对你了解和学习PHP中Ereg正则表达式有所帮助。
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号