Perl regex help - matching parentheses

Let's say I'm trying to match potentially multiple sets of parentheses. Is there a way in a regular expression to force a match of closing parentheses specifically in the number of the opening parentheses?
For example, if the string is "((foo bar))", I want to be able to say "match any number of opening parentheses, then any number of non-parentheses, followed by the same number of closing parentheses as the opening parentheses found, no more and no less."
This will be used for things like extracting "((bar))" from "(foo ((bar)))".

Thanks in advance!
-cvp

If you know the pattern you want to match that is what you would use:

my $str = '(foo ((bar)))';
my ($n) = $str =~ /(\Q((bar))\E)/;
print $n; 

\Q escapes the meta meaning of the parenthesis and treats them literally.

Thanks, but that wasn't my question; I already know how to escape parentheses.
My data is grouped in many variations of parentheses, sometimes doubled or tripled up. I wanted to know if there was a way to match all of those variations with one regex.
More examples:
extract "(bar)" from "(foo (bar))"
extract "(bar)" from "((foo (bar)) baz)"
extract "((foo))" from "(((foo)) bar)"
...etc.
The key thing here is matching the same number of closed parentheses as open parentheses. I just don't want to have to resort to using a stack or doing something like:

$string =~ /(\(+)[^)]*/;
$regex = ')' x length($1);
$match = $&;
if ($' =~ /$regex/) { $match .= $&; }
else { next; }
# etc.

I can't think of how to do all that with a single regexp. Maybe someone else can.

Apparently, someone else can't.
Thanks anyway.

Eat a peach.

You can try something like:

"([(]+\d+[)]+)"

this will detect the following examples:
((1) or ((1)) or ((((23)))

This is how I manage parentesis.
Hope this help!

#!/usr/bin/perl
$str="(one(((two)))abc)a((b))c(d)";
@arr=$str=~/\(+/g;
@tmp= sort {length($b) <=> length($a)}@arr;
# if would like to find the deepest '()', uncomment below part
#$num=length($tmp[0]);
#$exp1=qr/\({$num}[^\(\)]*\){$num}/;
#$str=~/$exp/;
# change below number can find whatever levels deep '()'
$num=1;
$exp2=qr/\({$num}[^\(\)]*\){$num}/;
@res=$str=~/$exp2/g;
print join "\n", @res;

The right answer here is in the perlre of modern perls:

                 The following pattern matches a parenthesized group:

                   $re = qr{
                              \(
                              (?:
                                 (?> [^()]+ )    # Non-parens without backtracking
                               |
                                 (??{ $re })     # Group with matching parens
                              )*
                              \)
                           }x;

Basically, (??{...}) embeds a regular expression (or a nested expression that chooses a regular expression) within another RE -- getting you recursive regular expressions.

If you have access to perl 5.10, you've got some other approaches available, as 5.10 has true recursive pattern matching without this kind of magic -- see here: perldelta - what is new for perl 5.10.0 - search.cpan.org