如何通过正则表达式匹配多个括号外的逗号
How to match comma outside multiple parentheses by regex
我用逗号分割了一个字符串,但不是在括号内,使用 preg_split
。我想到了
preg_split('#,(?![^\(]*[\)])#',$str);
除非嵌套括号前有逗号,否则效果很好。
适用于
$str = "first (1,2),second (child (nested), child2), third";
Array
(
[0] => first (1,2)
[1] => second (child (nested), child2)
[2] => third
)
但不适用于
$str = "first (1,2),second (child, (nested), child2), third";
Array
(
[0] => first (1,2)
[1] => second (child
[2] => (nested), child2)
[3] => third
)
看看忽略括号内的,
的要求,这个问题归结为确保括号平衡。如果任何 ,
位于 不平衡括号 内,我们将忽略它们,否则 ,
现在是我们拆分的分隔符。
为了收集这些 ,
之间的字符串,我们维护一个起始指针 $sub_start
来跟踪子字符串的起始索引,并在我们遇到有效的分隔符后更新它们 ,
.
片段:
<?php
function splitCommaBased($str){
$open_brac = 0;
$len = strlen($str);
$res = [];
$sub_start = 0;
for($i = 0; $i < $len; ++$i){
if($str[ $i ] == ',' && $open_brac == 0){
$res[] = substr($str, $sub_start, $i - $sub_start);
$sub_start = $i + 1;
}else if($str[ $i ] == '('){
$open_brac++;
}else if($str[ $i ] == ')'){
$open_brac--;
}else if($i === $len - 1){
$res[] = substr($str, $sub_start);
}
}
return $res;
}
print_r(splitCommaBased('first (1,2),second (child, (nested), child2), third'));
你可以使用匹配平衡括号的递归。然后使用 SKIP FAIL 并匹配要拆分的逗号。
(\((?:[^()]++|(?1))*\))(*SKIP)(*F)|,
看到一个regex demo。
例子
$str = "first (1,2),second (child, (nested), child2), third";
$pattern = "/(\((?:[^()]++|(?1))*\))(*SKIP)(*F)|,/";
print_r(preg_split($pattern, $str));
输出
Array
(
[0] => first (1,2)
[1] => second (child, (nested), child2)
[2] => third
)
我用逗号分割了一个字符串,但不是在括号内,使用 preg_split
。我想到了
preg_split('#,(?![^\(]*[\)])#',$str);
除非嵌套括号前有逗号,否则效果很好。
适用于
$str = "first (1,2),second (child (nested), child2), third";
Array
(
[0] => first (1,2)
[1] => second (child (nested), child2)
[2] => third
)
但不适用于
$str = "first (1,2),second (child, (nested), child2), third";
Array
(
[0] => first (1,2)
[1] => second (child
[2] => (nested), child2)
[3] => third
)
看看忽略括号内的,
的要求,这个问题归结为确保括号平衡。如果任何 ,
位于 不平衡括号 内,我们将忽略它们,否则 ,
现在是我们拆分的分隔符。
为了收集这些 ,
之间的字符串,我们维护一个起始指针 $sub_start
来跟踪子字符串的起始索引,并在我们遇到有效的分隔符后更新它们 ,
.
片段:
<?php
function splitCommaBased($str){
$open_brac = 0;
$len = strlen($str);
$res = [];
$sub_start = 0;
for($i = 0; $i < $len; ++$i){
if($str[ $i ] == ',' && $open_brac == 0){
$res[] = substr($str, $sub_start, $i - $sub_start);
$sub_start = $i + 1;
}else if($str[ $i ] == '('){
$open_brac++;
}else if($str[ $i ] == ')'){
$open_brac--;
}else if($i === $len - 1){
$res[] = substr($str, $sub_start);
}
}
return $res;
}
print_r(splitCommaBased('first (1,2),second (child, (nested), child2), third'));
你可以使用匹配平衡括号的递归。然后使用 SKIP FAIL 并匹配要拆分的逗号。
(\((?:[^()]++|(?1))*\))(*SKIP)(*F)|,
看到一个regex demo。
例子
$str = "first (1,2),second (child, (nested), child2), third";
$pattern = "/(\((?:[^()]++|(?1))*\))(*SKIP)(*F)|,/";
print_r(preg_split($pattern, $str));
输出
Array
(
[0] => first (1,2)
[1] => second (child, (nested), child2)
[2] => third
)