Parse parameters and values of smarty-like string in PHP

╄→гoц情女王★ 提交于 2021-02-05 08:00:48

问题


I'm trying to create simmiliar parser like smarty. For very small parts of code, and don't want to implement huge smarty-like parser.

What I came up with is: (?:([a-zA-Z0-9]+)=(?:([^\v '"]+)|"(.*?)"|'(.*?)')|([a-zA-Z0-9]+))

On https://regex101.com/r/l5FI5f/2/ it looks fine. Every match is either 1 or 2 entries + full match.

When I copy PHP code, things look different...

array (size=5)
  0 => string 'xddss='asdasda'' (length=15)
  1 => string 'xddss' (length=5)
  2 => string '' (length=0)
  3 => string '' (length=0)
  4 => string 'asdasda' (length=7)

Not sure where index 2 and 3 come from...


回答1:


You need to use branch reset groups:

(?|([a-zA-Z0-9]+)=(?|([^\v '"]+)|"(.*?)"|'(.*?)')|([a-zA-Z0-9]+))
  ^                 ^

See the regex demo.

From the reference:

Alternatives inside a branch reset group share the same capturing groups. The syntax is (?|regex) where (?| opens the group and regex is any regular expression. If you don't use any alternation or capturing groups inside the branch reset group, then its special function doesn't come into play. It then acts as a non-capturing group.



来源:https://stackoverflow.com/questions/49490427/parse-parameters-and-values-of-smarty-like-string-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!