我有以下字符串:
[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
答案 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
。参见演示。