php preg_match找到所有部分

时间:2012-05-02 11:04:06

标签: regex

我需要解析字符串

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

进入

array("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
    )*?)\ *\}!          

我明白了:

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"
  }
}

我如何获得参数数组,而不是最后一个?

1 个答案:

答案 0 :(得分:0)

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