下面图老师小编要向大家介绍下关于php正则表达式的两点备注,看起来复杂实则是简单的,掌握好技巧就OK,喜欢就赶紧收藏起来吧!
【 tulaoshi.com - Web开发 】
severaltipsaboutRegularExpressions
1.processfor"greedy"
Bydefault,thequantifiersare"greedy",thatis,they
matchasmuchaspossible(uptothemaximumnumberofper-
mittedtimes),withoutcausingtherestofthepatternto
fail.Theclassicexampleofwherethisgivesproblemsisin
tryingtomatchcommentsinCprograms.Theseappearbetween
thesequences/*and*/andwithinthesequence,individual
*and/charactersmayappear.AnattempttomatchCcom-
mentsbyapplyingthepattern
/*.**/
tothestring
/*firstcommand*/notcomment/*secondcomment*/
fails,becauseitmatchestheentirestringduetothe
greedinessofthe.*item.
However,ifaquantifierisfollowedbyaquestionmark,
thenitceasestobegreedy,andinsteadmatchestheminimum
numberoftimespossible,sothepattern
/*.*?*/
小结:
?与/U有类似功能,但同时出现彼此抵消
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)如下:
$a="asdf/*asdfaldsfasdf*/asfdasldf;kfldsj*/asfddsaf";
$pattern="//*.*?*//";
//$pattern="//*.**//U";
//$pattern="//*.*?*//U";
preg_match($pattern,$a,$match);
print_r($match);
?
2.Assertions
w+(?=;)
matchesawordfollowedbyasemicolon,butdoesnotinclude
thesemicoloninthematch,and
foo(?!bar)
matchesanyoccurrenceof"foo"thatisnotfollowedby
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)"bar".Notethattheapparentlysimilarpattern
小结:
(?!)只前向判断匹配,如bar(?!foo),而(?!foo)bar没有意义
(?
来源:http://www.tulaoshi.com/n/20160219/1613185.html
看过《关于php正则表达式的两点备注》的人还看了以下文章 更多>>