perlquestion
shemp
Hi all, here for the zeroth time this year - finally.
<P>
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.
<P>
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:
<CODE>
$string =~ s/(\d)-([A-Z])/$1$2/ig; # my old way
$string =~ s/(?<=\d)-(?=[A-Z])//ig; # my new way
</CODE>
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.
<P>
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)
<P>
So flame me, or offer $0.02 or whatever.
<P>
thanks,<BR>
sean