如果它很简单,可能是递归正则表达式(这是在 Perl 中)。我敢肯定,使用彻底完成它的语言解析器可以更好地处理它。
$str = 'some stuff F( g(x), tx, , 44, Y(hh()) , 99, b())';
$open = '\b\w+\s*';
$regex = qr~
( # 1
($open) # 2
[(]
( # 3
(?: (?> (?: (?!$open[(] | [)] ) . )+ )
| (?1)
)*
)
[)]
)
~xs;
print "Before: ", $str, "\n";
print "After: ", parse_func ( $str ), "\n";
###
sub parse_func {
my ($core) = @_;
$core =~ s/$regex/ "[$2," . (parse_func( $3 )) . "]" /eg;
return $core;
}
输出
Before: some stuff F( g(x), tx, , 44, Y(hh()) , 99, b())
After: some stuff [F, [g,x], tx, , 44, [Y,[hh,]] , 99, [b,]]