shemp has asked for the wisdom of the Perl Monks concerning the following question:
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.$string =~ s/(\d)-([A-Z])/$1$2/ig; # my old way $string =~ s/(?<=\d)-(?=[A-Z])//ig; # my new way
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.
thanks,
sean
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: lookahead / lookbehind vs other regex methods
by BrowserUk (Patriarch) on Jan 07, 2004 at 22:13 UTC | |
Re: lookahead / lookbehind vs other regex methods
by duff (Parson) on Jan 07, 2004 at 22:07 UTC | |
Re: lookahead / lookbehind vs other regex methods
by ysth (Canon) on Jan 08, 2004 at 00:17 UTC | |
Re: lookahead / lookbehind vs other regex methods
by ambrus (Abbot) on Jan 09, 2004 at 12:05 UTC |