动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   PHP → 阅读文章

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

作者来源: 
阅读 1579 人次 , 2006-3-29 4:17:00 


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有类似功能,但同时出现彼此抵消
  
   如下:
      $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
  
   小结:
   (?!)只前向判断匹配,如bar(?!foo),而(?!foo)bar没有意义
   (?

 本文Tags正则表达式  
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:PHP和正则表达式
· 下一篇:正则表达式在PHP中的应用
· 聊天室处理异常离线的方法
· 用PHP做短信开发
· NIS 函数库
· 分页显示ODBC数据库记录的类
· PHP安全配置(4)


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077