http://www.perl.com/pub/2003/06/06/regexps.html
####
$ cat p2.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)/;
print "----------\n";
print "$stuff\n";
print "----------\n";
print $1 . "\n";
print "----------\n";
####
$ ./p2.pl
----------
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.
----------
(we go( in( and in (&stop)(awhile) ( further ))) but still (here) )
----------
$
####
$ cat p3.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 "----------\n";
print "$stuff\n";
print "----------\n";
print $1 . "\n";
print "----------\n";
print $2 . "\n";
print "----------\n";
$ ./p3.pl
----------
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.
----------
(we go( in( and in (&stop)(awhile) ( further ))) but still (here) )
----------
----------
$