text: (1+2(123+(888)3)ddf)wwww(sdf(sdfdsf)gggg) Match: 1+2 123+ 888 3 ddf sdf sdfdsf gggg
匹配代码:
#!/usr/bin/perl use strict; use warnings; use re ‘eval‘; my $str= ‘(1+2(123+(888)3)ddf)wwww(sdf(sdfdsf)gggg)‘; my @arr; my $re = qr/(?{local $a=0})(?>(?:([^()]+)(?{push @arr,$1})| #将匹配到得内容push到@arr #固化分组,减少回朔。 \((?{$a++})| #遇到开括号$a+1 \)(?(?{$a!=0})(?{$a--})| #如果$a不等于0则遇到闭括号$a-1 (?!)))*)(?(?{$a!=0})(?!))/x; # 否则继续匹配,如果继续遇到$a不等于0 继续匹配到行尾 1 while($str =~ /\($re\)/g); print join("\n",@arr);
时间: 2024-11-10 05:30:39