http://qs1969.pair.com?node_id=1100528


in reply to Re^2: regex to rename last _ with ,
in thread regex to rename last _ with ,

A couple of reasons. First, I can never remember the exact syntax of the look-around assertions, so I have to perldoc perlre each time I use one. This makes it advanced in my mind. I guess this is highly subjective, so your point is taken.

Second, the more features are used in a regular expression, the more there is to translate when you want to use it in another tool. Doing without look-ahead brings the RE to a lower common denominator, so to speak.

Replies are listed 'Best First'.
Re^4: regex to rename last _ with ,
by AppleFritter (Vicar) on Sep 14, 2014 at 14:57 UTC

    First, I can never remember the exact syntax of the look-around assertions

    They're fairly mnemonic, actually:

    • (?=PATTERN) - positive lookahead. Mnemonic: the following data equals (=; i.e. matches) the supplied pattern.
    • (?!PATTERN) - negative lookahead. Mnemonic: the following data does not (!) match the pattern.
    • (?<=PATTERN) - positive lookbehind. Mnemonic: the preceding (<) data equals (=; i.e. matches) the pattern.
    • (?<!PATTERN) - negative lookbehind. Mnemonic: the preceding (<) data does not (!) match the pattern.

    Personally I might've chosen (?>=PATTERN) and (?>!PATTERN) for lookahead assertions to make the syntax of these symmetric in direction as well (though that would've clashed with non-backtracking patterns), but even this way I still think these are fairly easy to remember.

      Look-ahead is used much more frequently than lookbehind, hence the shorter syntax. Perl is quite heavily Huffman coded.

      Look-behind is actually almost useless, since variable-length look-behind isnít supported. It can be approximated using \K