无法匹配多个"属性" in" [shortcode attr = value attr = value]" PHP中的REGEX字符串

时间:2015-06-26 12:46:20

标签: php regex

我有以下字符串:

[jeans size="L"] [jeans color="blue" size="XL"]

我在PHP的preg_match_all()中运行以下正则表达式:

#\[(?P<tag_name>\w+)(?:\s*(?P<attr>\w+)\s?[=,:]\s?["\'](?P<value>\w*)["\'])*\s*]#igs

我尝试了许多正则表达式的变体,并且似乎无法获得给定短代码的多个属性,例如第二个中的颜色和大小。每个标记只检索最后一个属性。

这是一个小提琴:https://regex101.com/r/hF1tS1/5

enter image description here

1 个答案:

答案 0 :(得分:1)

\[(?P<tag_name>\w+)\K|\G(?!^)(?:\s*(?P<attr>\w+)\s?[=,:]\s?["\'](?P<value>\w*)["\'])

您可以在此处使用\G assert position at the end of the previous match or the start of the string for the first match。参见演示。

https://regex101.com/r/yG7zB9/33