I’m hoping that someone can give me some pointers. I need to match the
content within the parentheses only, from the following string (this is
an example string, but the structure is consistent):
d_(1)+d_(23)
So far I have matched something else using \b to match a closing tic
(`), but that doesn’t seem to work here:
perl’s regexes have a recursive feature, which can solve your problem.
Want to call an external script? In fact, perl has a library which
contains regexes for hard to solve problems, so you don’t even have to
write the regex yourself. Here’s what it looks like:
$regex = qr{
(
(?:
(?> [^()]++ ) # Non-parens without
backtracking
|
(??{ $regex }) # Group with matching
parens
)*
)
}x;
I have upgraded the regular expression somewhat, to enclose other
similar situations, but I still have the problem of not getting the last
parenthesis (the one completely ending the statement) included in the
expression (while not grabbing anything beyond the parentheses).
perl’s regexes have a recursive feature, which can solve your problem.
Want to call an external script?
Not necessary: Ruby has it, too.
irb(main):002:0> %r{(? ( (?:\g|[^()]+)* ) )}x
How come the perl regex uses the non-backtracking ++, and yours doesn’t
appear to do the equivalent? Hmmm… I think the (?> also means
non-backtracking.