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
to match strings which have balanced parentheses, for example./^([^()]|\((?1)*\))*$/
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: More Power to your Regex
by Juerd (Abbot) on Apr 02, 2002 at 00:28 UTC | |
by robin (Chaplain) on Apr 02, 2002 at 08:52 UTC | |
by Juerd (Abbot) on Apr 02, 2002 at 10:04 UTC | |
Re: More Power to your Regex
by blackflag (Novice) on Apr 01, 2002 at 23:48 UTC | |
by blackflag (Novice) on Apr 02, 2002 at 21:08 UTC | |
Re: More Power to your Regex
by belg4mit (Prior) on Apr 02, 2002 at 00:31 UTC | |
by robin (Chaplain) on Apr 02, 2002 at 08:36 UTC | |
Re: More Power to your Regex
by abstracts (Hermit) on Apr 02, 2002 at 03:43 UTC | |
by robin (Chaplain) on Apr 02, 2002 at 08:49 UTC | |
by demerphq (Chancellor) on Apr 02, 2002 at 10:08 UTC |
Back to
Meditations