Home > Backend Development > PHP Tutorial > 正则表达式偏题

正则表达式偏题

WBOY
Release: 2016-06-13 12:52:17
Original
916 people have browsed it

正则表达式难题

本帖最后由 xjl756425616 于 2013-03-07 21:02:34 编辑
<?php<br />
$a="a11ba12b";<br />
preg_match_all("/(a[^b]+b){2}/is",$a,$tmp);<br />
var_dump($tmp);<br />
?>
Copy after login


得出的结果是

array(2) {<br />
  [0]=><br />
  array(1) {<br />
    [0]=><br />
    string(8) "a11ba12b"<br />
  }<br />
  [1]=><br />
  array(1) {<br />
    [0]=><br />
    string(4) "a12b"<br />
  }<br />
}
Copy after login



现在要问的是,如何操作使得使用{}中括号表示法,但是[1]中是= a11b,而不是 [1]=a12b,


------解决方案--------------------
\G?

<br />
    $a="a11ba11b";<br />
    preg_match_all("/\G(a[^b]+b){2}/i",$a,$tmp);<br />
    var_dump($tmp);<br />
Copy after login

------解决方案--------------------
..baoqian
貌似错的
------解决方案--------------------
不如指定贪婪模式
preg_match_all("/(a[^b]+b)+/iU",$a,$tmp);

坐等大牛回答。
------解决方案--------------------
这样? 你到底要干啥子吗

<br />
<?php<br />
<br />
$a="a11ba12b";<br />
<br />
preg_match_all("/(a[^b]+b)a[^b]+b/is",$a,$tmp);<br />
<br />
var_dump($tmp);<br />
Copy after login

------解决方案--------------------
引用:
我就是在研究大括号,哈哈

这有什么可研究的?规矩就是规矩
{ 最少/最多数量限定开始 
} 最少/最多数量限定结束 

虽然规则有时不尽合理,但并不会因你而改变
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template