[正则]分组匹配求解

MoontoC 2011-06-23 12:10:17
$in = '<x xx, x1, x2, x3>';
preg_match('#^<x ([a-z\d]+), ([a-z\d]+)(?:, ([a-z\d]+))*>$#i', $in, $matches);
var_dump($matches);


得到的结果:
array(4) {
[0]=> string(18) ""
[1]=> string(2) "xx"
[2]=> string(2) "x1"
[3]=> string(2) "x3" }


我希望的结果:
array(4) {
[0]=> string(18) ""
[1]=> string(2) "xx"
[2]=> string(2) "x1"
[3]=> string(2) "x2"
[4]=> string(2) "x3" }


正则中我希望重复匹配到x1到x3, 并将它们放入数组, 但是x2被忽略了,,, 求解
...全文
103 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
MoontoC 2011-06-24
  • 打赏
  • 举报
回复
结贴, 放弃,
一起混吧 2011-06-23
  • 打赏
  • 举报
回复

preg_match('/<\w (\w+), (\w+), (\w+), (\w+)>/i', $in, $matches);
var_dump($matches);

MoontoC 2011-06-23
  • 打赏
  • 举报
回复
感谢楼上两位的preg_match_all结果, 虽然是有效, 但没办法对应我的实际情况, 我并不是就像匹配一堆东西里面用逗号分隔的一坨, 而是<tag xxx, xxx, xxx>标签是固定的, 并且有多个不同类型, 不同语法的tag

然后还要$rules = '#\{' . join( '\}|\{', $rules ) . '\}#i';

最后用preg_replace_callback来连续处理多个tag内容, 所以没办法使用_all, 上面的例子, 我可能说的不全面吧_-
床上等您 2011-06-23
  • 打赏
  • 举报
回复

$str = <<<TEXT
<x xx, x1, x2, x3>
TEXT;

preg_match_all('#([a-z\d]+,?)+#i', $str, $matches);
echo '<pre>';var_dump($matches[1]);
/*
输出结果:
array(5) {
[0]=>
string(1) "x"
[1]=>
string(3) "xx,"
[2]=>
string(3) "x1,"
[3]=>
string(3) "x2,"
[4]=>
string(2) "x3"
}


*/
xuzuning 2011-06-23
  • 打赏
  • 举报
回复
$in = '<x xx, x1, x2, x3>';
preg_match_all('/(\w+){2,}/i', $in, $matches);
var_dump($matches[0]);

array(4) {
[0]=>
string(2) "xx"
[1]=>
string(2) "x1"
[2]=>
string(2) "x2"
[3]=>
string(2) "x3"
}
一起混吧 2011-06-23
  • 打赏
  • 举报
回复
什么匹配100次?
MoontoC 2011-06-23
  • 打赏
  • 举报
回复
匹配100次呢?

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧