问题
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