I've just implemented an experimental regex extension, which allows you
to do things like match balanced parentheses, without having to embed any
code in your regex. The implementation
isn't in perl (yet?), but
this is still a firm Perl topic, and it raises questions about what
features Perl's regexes ought to have.
I wrote a web page
about it, but here's the rough idea. You can treat each parenthesised group
in your regex as if it were a little subroutine, and make calls to it from
within your regex. The power comes from the fact that you can make recursive
calls, so you can write
/^([^()]|\((?1)*\))*$/
to match strings which have balanced parentheses, for example.
My favourite so far is a regex to detect palindromic sentences, ignoring
spacing and punctuation.
/^\W*(?:((.)\W*(?1)\W*\2|)|((.)\W*(?3)\W*\4|\W*.\W*))\W*$/i
If you want to try it out, follow the instructions
here.
Let me know what you think.