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, 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.
#!/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.