给定输入字符串“ othertext?.abc.de.fgh.i moretext ”和模式'/\?(\.[a-zA-Z]*)*/',我正在尝试使用函数preg_match_all
来获取数组与元素匹配名为.abc,由Matchi.com提供回到,.fgh和.I但只?.abc.de.fgh.i和。我是匹配的。(测试)
您可以利用\G
锚点
(?:\?|\G(?!^))\K\.[a-zA-Z]+
部分:
(?:
非捕获组
\?
比赛 ?
|
要么\G(?!^)
在上一场比赛的末尾而不是在开始时断言位置)
封闭组\K\.[a-zA-Z]+
忘记当前匹配的内容,并匹配.
和1个字符a-zA-Z$re = '/(?:\?|\G(?!^))\K\.[a-zA-Z]+/';
$str = 'othertext ?.abc.de.fgh.i moretext';
preg_match_all($re, $str, $matches);
var_dump($matches[0]);
输出量
array(4) {
[0]=>
string(4) ".abc"
[1]=>
string(3) ".de"
[2]=>
string(4) ".fgh"
[3]=>
string(2) ".i"
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句