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
/*.*?*/
Summary:
? and/u have similar functions, but simultaneously offset each other
As follows:
$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
"Bar". Notethattheapparentlysimilarpattern
Summary:
(?!) Only forward to the judgment match, such as bar (?! Foo), and (?! Foo) bar doesn't make sense
(?
http://www.bkjia.com/PHPjc/532281.html www.bkjia.com true http://www.bkjia.com/PHPjc/532281.html techarticle severaltipsaboutregularexpressions 1.processfor "greedy" bydefault,thequantifiersare "greedy", thatis,they Matchasmuchaspossible (uptothemaximumnumberofper-mittedtimes), withoutcausing ...