I have been looking for a way to match balanced parenthesis in a regex and found a way in Perl, that uses a recursive regular expression:
my $re;
$re = qr{
\(
(?:
(?> [^()]+ ) # Non-parens without backtracking
|
(??{ $re }) # Group with matching parens
)*
\)
}x;
from the perl regular expression site
.
Is there a way to do this in Ruby or a similar language?
UPDATE:
For those interested here are some interesting links:
Oniguruma manual – from Sawa’s answer.
Pragmatic Programmers’ Ruby 1.9 Regular Expressions Sample Chapter
Yes. With oniguruma regex engine, which is built in in Ruby 1.9, and is installable on Ruby 1.8, you can do that. You name a subregex with
(?<name>...)or(?'name'...). Then you call a subregex with\g<name>or\g'name'within the same regex. So your regex translated to oniguruma regex will be:Also note that multi-byte string module in PHP >=5 uses oniguruma regex engine, so you will be able to do the same.
The manual for oniguruma is here.