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:
? Similar to/U, but at the same time 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 judgment matches, such as bar (?! Foo), and (?! Foo) bar is meaningless.
(?