|P is for Practical|
lookahead / lookbehind vs other regex methodsby shemp (Deacon)
|on Jan 07, 2004 at 22:00 UTC||Need Help??|
shemp has asked for the
wisdom of the Perl Monks concerning the following question:
Hi all, here for the zeroth time this year - finally.
I've started becoming a big fan of lookahead and lookbehind assertions in regexs, and im starting to try different ways to perform certain tasks. I'm wondering what people have to say regarding what's preferable in certain situations, or if this question degenerates into a programming style holy war.
So, one instance is where you want to remove a dash from a string if its preceeded by a number and succeeded by a letter. (trivial example). Here's a couple ways to do this:
Unless im missing something, those lines should both accomplish the exact same thing. Im really becoming a fan of the lookahead / lookbehind version, it seems like it should run faster than the $1$2 way, but i know virtually nothing of the underlying workings of the regex engine.
There are other similar situations that i am in limbo over using lookahead / lookbehind, but the trivial cases all boil down to the above (more or less)
So flame me, or offer $0.02 or whatever.