$ cat p7.pl #!/opt/perl5.16/bin/perl use strict; use warnings; our $paren = qr/ # Need declared variable with use strict. \( ( [^()]+ # Not parens | (??{our $paren}) # Another balanced group (not interpolated yet) )* \) /x; # 'x' means ignore whitespace, comments. my $stuff = "On the outside now then (we go( in( and in (&stop)(awhile) ( further ))) but still (here) ) and now ((for a while)) we are out again."; $stuff =~ /($paren)[^()]*($paren)/; print "-original-\n"; print "$stuff\n"; print "1---------\n"; print 'X' . $1 . 'X' . "\n"; print "2---------\n"; print 'X' . $2 . 'X' . "\n"; print "3---------\n"; print 'X' . $3 . 'X' . "\n"; print "4---------\n"; print 'X' . $4 . 'X' . "\n"; print "5---------\n"; print 'X' . $5 . 'X' . "\n"; print "6---------\n"; print 'X' . $6 . 'X' . "\n"; print "----------\n"; $ ./p7.pl -original- On the outside now then (we go( in( and in (&stop)(awhile) ( further ))) but still (here) ) and now ((for a while)) we are out again. 1--------- X(we go( in( and in (&stop)(awhile) ( further ))) but still (here) )X 2--------- X X 3--------- X((for a while))X 4--------- X(for a while)X 5--------- Use of uninitialized value $5 in concatenation (.) or string at ./p7.pl line 31. XX 6--------- Use of uninitialized value $6 in concatenation (.) or string at ./p7.pl line 33. XX ---------- $