Regex php preg_match查找所有部分
我需要解析字符串Regex php preg_match查找所有部分,regex,Regex,我需要解析字符串 {name "some" 'words' `or` '}\'other staff' in:0 line:`\`} 进入 ,但preg只返回模板的一个匹配项,我可以做些什么来获取所有匹配项吗? 这是我的正则表达式: !\{ (\#?[a-zA-Z0-9\_]{1,16}) (\.[a-zA-Z0-9\_]{1,16})? (?P<args>(?P<arg> \ +([a-zA-Z0-9\_]{1,16}\:)?
{name "some" 'words' `or` '}\'other staff' in:0 line:`\`}
进入
,但preg只返回模板的一个匹配项,我可以做些什么来获取所有匹配项吗?
这是我的正则表达式:
!\{
(\#?[a-zA-Z0-9\_]{1,16})
(\.[a-zA-Z0-9\_]{1,16})?
(?P<args>(?P<arg>
\ +([a-zA-Z0-9\_]{1,16}\:)?
\'(\\\\|\\\'|[^\'])*?\'| //argument in singlequote
\"([^\\\"]|\\.)*?\"| //argument in doublequote
\`[^\`]*?\`| //argument in backquote
[0-9]*(\.[0-9]*)? //argument numeral
)*?)\ *\}!
如何获取参数数组,而不是最后一个?我认为无法在preg中解析它,我需要使用字符串函数来编写它。我甚至不打算尝试解析这个庞大的正则表达式。但我可以告诉你出了什么问题:你在重复一个捕获组,这意味着每次重复该组时,上一个匹配都会被覆盖。没错,问题是我如何做到这一点。args计数没有限制。所以我只能重复得到它们,但我不能得到每个部分。mb我应该再次解析已建立的列表吗?
!\{
(\#?[a-zA-Z0-9\_]{1,16})
(\.[a-zA-Z0-9\_]{1,16})?
(?P<args>(?P<arg>
\ +([a-zA-Z0-9\_]{1,16}\:)?
\'(\\\\|\\\'|[^\'])*?\'| //argument in singlequote
\"([^\\\"]|\\.)*?\"| //argument in doublequote
\`[^\`]*?\`| //argument in backquote
[0-9]*(\.[0-9]*)? //argument numeral
)*?)\ *\}!
array(1) {
[0]=>
array(12) {
[0]=>
string(57) "{name "some" 'words' `or` '}\'other staff' in:0 line:`\`}"
[1]=>
string(4) "name"
[2]=>
string(0) ""
["args"]=>
string(51) " "some" 'words' `or` '}\'other staff' in:0 line:`\`"
[3]=>
string(51) " "some" 'words' `or` '}\'other staff' in:0 line:`\`"
[4]=>
string(9) " line:`\`"
["arg"]=>
string(8) "line:`\`" //last argument
[5]=>
string(8) "line:`\`"
[6]=>
string(5) "line:"
[7]=>
string(3) "`\`"
[8]=>
string(1) "f"
[9]=>
string(1) "e"
}
}