perl 匹配嵌套括号问题

 Text as follows:

(1+2(123+(888)3)ddf)wwww(sdf(sdfdsf)gggg)

Match:

1+2 123+ 888 3 ddf sdf sdfdsf gggg

 

 

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

你可能感兴趣的:(正则匹配嵌套问题,嵌套匹配)