PHP头条
热点:

通配符(lookarounds):断言某些字符串中某些字符的存在与否!

lookarounds分两种:lookaheads(正向预查 ?=)和lookbehinds(反向预查?<=)。

格式:

正向预查:(?=) 相对应的 (?!)表示否定意思;反向预查:(?<=) 相对应的 (?<!)表示否定意思

前后紧跟字符

  1. $regex = '/(?<=c)d(?=e)/';  /* d 前面紧跟c, d 后面紧跟e*/ 
  2. $str = 'abcdefgk';   
  3. $matches = array();   
  4.     
  5. if(preg_match($regex$str$matches)){   
  6.     var_dump($matches);   
  7. }   
  8.     
  9. echo "\n"

否定意义:

  1. $regex = '/(?<!c)d(?!e)/';  /* d 前面不紧跟c, d 后面不紧跟e*/ 
  2. $str = 'abcdefgk';   
  3. $matches = array();   
  4.     
  5. if(preg_match($regex$str$matches)){   
  6.     var_dump($matches);   
  7. }   
  8.     
  9. echo "\n"

字符宽度:零

验证零字符代

  1. $regex = '/HE(?=L)LO/i';   
  2. $str = 'HELLO';   
  3. $matches = array();   
  4.     
  5. if(preg_match($regex$str$matches)){   
  6.     var_dump($matches);   
  7. }   
  8.     
  9. echo "\n"

打印不出结果!

  1. $regex = '/HE(?=L)LLO/i';   
  2. $str = 'HELLO';   
  3. $matches = array();   
  4.     
  5. if(preg_match($regex$str$matches)){   
  6.     var_dump($matches);   
  7. }   
  8.     
  9. echo "\n"

能打印出结果!

说明:(?=L)意思是HE后面紧跟一个L字符。但是(?=L)本身不占字符,要与(L)区分,L)本身占一个字符。


www.phpzy.comtrue/php/4053.htmlTechArticle通配符(lookarounds):断言某些字符串中某些字符的存在与否! lookarounds分两种:lookaheads(正向预查 ?=)和lookbehinds(反向预查?=)。 格式: 正向预查:(?=) 相对应的 (...

相关文章

相关频道:

PHP之友评论

今天推荐