I am curious, does Mathematica have the capability to match strings
involving balanced parentheses? In particular, would it be possible
to translate the following Perl code into Mathematica?
#!/usr/bin/perl
my $bal;
$bal = qr /[(] # match opening paren
[^()]* # match stuff that doesn't involve
parens
(?:(??{ $bal }) [^()]* )* # matched balanced parens + non-
# parens characters
[)]/x; # match closing paren
# usage example: replace the argument to sqrt() function, where the
argument is a string containing balanced
# parens
s/(sqrt)( (??{ $bal }) )/Sqrt\[\2\]/gx;