2015-11-01 106 views
0

我使用php,我需要用正则表达式拆分字符串(xpath)。 我只需要一个正则表达式来分割下面的“输入”。我希望有一个人可以帮助我。正则表达式拆分xpath


输入: /路径/到/节点/测试

输出:

$result = array(
    "path", 
    "to", 
    "node", 
    "test", 
); 

输入: /路径/到[子= “string”AND sub2 =“string2”]/node/test

输出:

$result = array(
    'path', 
    'to[sub="string" AND sub2="string2"]', 
    'node', 
    'test', 
); 

输入: /路径/到[子/路径/要= “字符串”] /节点/测试

输出:

$result = array(
    'path', 
    'to[sub/path/to="string"]', 
    'node', 
    'test', 
); 

以前谢谢!

问候 萨沙

+0

尝试我的编辑和 –

回答

-1

只需使用explode

$str = explode('/',yourString) 

或者,如果你有,你要不理你可以试试下面的正则表达式内/ onnly内[]

preg_match_all("/\[(?:[^\[\]]|(?R))+\]|[^\[\]\/]+/", $input, $matches); 

例如:

$input = '/path/to[sub/path/to="string"]/node/test'; 
preg_match_all("/\[(?:[^\[\]]|(?R))+\]|[^\[\]\/]+/", $input, $matches); 
var_dump($input); 
var_dump($matches); //this will give you expected output 

说明:

我们捕捉2种在这里:

  1. /\[(?:[^\[\]]|(?R))+\]匹配方括号之间的任何递归
  2. [^\[\]\/]+比赛不是由[]
任何字符序列
+1

不,它不会工作w埃尔。 http://melpon.org/wandbox/permlink/MiCPLRelWfEOmuEy – MikeCAT

+0

@MikeCAT尝试替代正则表达式 –

+0

通过新的正则表达式,“”到“和”“[sub/path/to =”string“]”'被分割,但在实际中他们不是。 http://melpon.org/wandbox/permlink/JEQfPQEKgJfzLhHt – MikeCAT