[英]Split xpath with regular expression
I use php and I need to split a string (xpath) with regular expression. 我使用php,我需要使用正则表达式拆分字符串(xpath)。 I need just one regex to split to following "inputs".
我只需要一个正则表达式即可拆分为以下“输入”。 I hope someone can help me.
我希望有一个人可以帮助我。
Input: /path/to/node/test 输入: / path / to / node / test
Output: 输出:
$result = array(
"path",
"to",
"node",
"test",
);
Input: /path/to[sub="string" AND sub2="string2"]/node/test 输入: / path / to [sub =“ string” AND sub2 =“ string2”] / node / test
Output: 输出:
$result = array(
'path',
'to[sub="string" AND sub2="string2"]',
'node',
'test',
);
Input: /path/to[sub/path/to="string"]/node/test 输入: / path / to [sub / path / to =“ string”] / node / test
Output: 输出:
$result = array(
'path',
'to[sub/path/to="string"]',
'node',
'test',
);
Thanks before! 之前谢谢!
Best regards Sascha 最好的问候萨沙
Just use explode
只需使用
explode
$str = explode('/',yourString)
Or if you have inner /
onnly inside []
which you want to ignore you can try the following regex: 或者,如果您想忽略内部
[]
on []
/
内部[]
,则可以尝试以下正则表达式:
preg_match_all("/\[(?:[^\[\]]|(?R))+\]|[^\[\]\/]+/", $input, $matches);
Example: 例:
$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
Explanation: 说明:
We're capturing 2 types here: 我们在这里捕获2种类型:
/\\[(?:[^\\[\\]]|(?R))+\\]
matches anything between square brackets recursively /\\[(?:[^\\[\\]]|(?R))+\\]
递归匹配方括号之间的任何内容 [^\\[\\]\\/]+
matches any char-sequence not consisting of [
]
[^\\[\\]\\/]+
匹配不包含[
]
任何字符序列
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.