2012-05-02 29 views
1

我需要解析字符串PHP的preg_match找到模板各地

{name "some" 'words' `or` '}\'other staff' in:0 line:`\`} 

array("some","words","or","}'other staff","in"=>0, "line"=>"\\") 

,但预浸只返回一个比赛,我能做些什么来获取所有的比赛? 这里是我的正则表达式:

!\{ 
    (\#?[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" 
    } 
} 

我怎样才能得到的参数数组,而不是最后一个?

+0

我甚至不会尝试解析正则表达式的怪物。但我可以告诉你发生了什么问题:你正在重复一个捕获组,这意味着在该组的每次重复中,前一个匹配将被覆盖。 –

+0

多数民众赞成如此,问题是我该如何做到这一点。参数计数没有限制。所以我可以只重复他们,但我不能得到每个部分。我应该再次分析一下创建的列表吗? –

回答

0

我认为没有办法在preg中解析它,我需要使用字符串函数来编写它。