关于php正则表达式的两点备注

发表于:2007-06-21来源:作者:点击数: 标签:
下一页 1 2 severaltipsaboutRegularExpressions 1.processfor"greedy" Bydefault,thequ ant ifiersare"greedy",thatis,they matchasmuchaspossible(uptothemaximumnumberofper- mittedtimes),withoutcausingtherestofthepatternto fail.Theclassicexampleof

下一页 1 2 

     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
  
  /\*.*?\*/
  

原文转自:http://www.ltesting.net