各位好:我有以下一个正则表达式模式:
#
\(
(?P<outside>
(?P<inside>[^()]+)
|
(?R)
)*
\)
#x目标字符串为: (first_part)(second_part(third_part))匹配的字符串有两部分:
(first_part)
(second_part(third_part))
但是, 为何第二部分匹配的时候, <outside> 这组结果是 (third_part) ?而不是 second_part(third_part) 整个部分?附上 PHP 源代码和运行结果:
<?php
$reg = '#
\(
(?P<outside>
(?P<inside>[^()]+)
|
(?R)
)*
\)
#x'; $m = preg_match_all($reg, '(first_part)(second_part(third_part))', $matches);
var_dump($matches);
?>The result:
array(3) {
[0]=>
array(2) {
[0]=>
string(12) "(first_part)"
[1]=>
string(25) "(second_part(third_part))"
}
["outside"]=>
array(2) {
[0]=>
string(10) "first_part"
[1]=>
string(12) "(third_part)"
}
["inside"]=>
array(2) {
[0]=>
string "first_part"
[1]=>
string "second_part"
}
}谢谢先!
#
\(
(?P<outside>
(?P<inside>[^()]+)
|
(?R)
)*
\)
#x目标字符串为: (first_part)(second_part(third_part))匹配的字符串有两部分:
(first_part)
(second_part(third_part))
但是, 为何第二部分匹配的时候, <outside> 这组结果是 (third_part) ?而不是 second_part(third_part) 整个部分?附上 PHP 源代码和运行结果:
<?php
$reg = '#
\(
(?P<outside>
(?P<inside>[^()]+)
|
(?R)
)*
\)
#x'; $m = preg_match_all($reg, '(first_part)(second_part(third_part))', $matches);
var_dump($matches);
?>The result:
array(3) {
[0]=>
array(2) {
[0]=>
string(12) "(first_part)"
[1]=>
string(25) "(second_part(third_part))"
}
["outside"]=>
array(2) {
[0]=>
string(10) "first_part"
[1]=>
string(12) "(third_part)"
}
["inside"]=>
array(2) {
[0]=>
string "first_part"
[1]=>
string "second_part"
}
}谢谢先!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货