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

撒打算阿斯601

撒打算阿斯601

2016-02-19 16:48

下面图老师小编要向大家介绍下关于php正则表达式的两点备注,看起来复杂实则是简单的,掌握好技巧就OK,喜欢就赶紧收藏起来吧!

  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

  /*.*?*/

(本文来源于图老师网站,更多请访问https://m.tulaoshi.com/webkaifa/)

  小结:

  ?与/U有类似功能,但同时出现彼此抵消

(本文来源于图老师网站,更多请访问https://m.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

  "bar".Notethattheapparentlysimilarpattern

  小结:

  (?!)只前向判断匹配,如bar(?!foo),而(?!foo)bar没有意义

  (?

展开更多 50%)
分享

猜你喜欢

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

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

正则表达式

Web开发
正则表达式

s8lol主宰符文怎么配

英雄联盟 网络游戏
s8lol主宰符文怎么配

正则表达式口诀 正则表达式学习工具

Web开发
正则表达式口诀 正则表达式学习工具

php的正则表达式专题

Web开发
php的正则表达式专题

lol偷钱流符文搭配推荐

英雄联盟 网络游戏
lol偷钱流符文搭配推荐

PHP 正则表达式分析RSS

Web开发
PHP 正则表达式分析RSS

[PHP]常用正则表达式收集

Web开发
[PHP]常用正则表达式收集

lolAD刺客新符文搭配推荐

英雄联盟
lolAD刺客新符文搭配推荐

MVC 设计模式带来更好的软件结构和代码重用

MVC 设计模式带来更好的软件结构和代码重用

Oracle 10g Release2新功能之变化通知

Oracle 10g Release2新功能之变化通知
下拉加载更多内容 ↓