Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: regex to rename last _ with ,

by AnomalousMonk (Archbishop)
on Sep 13, 2014 at 17:28 UTC ( [id://1100500]=note: print w/replies, xml ) Need Help??


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

This can be done without advanced features like lookaheads ...

It seems strange to me to see a regex lookahead (or, in general, a look-around) assertion described as 'advanced'. It's as if one had said "X can be done without advanced features like lexical variables or hard references." Present, I believe (although I haven't taken the trouble to confirm this), since version 5.0, this is a feature I consider part of the 'baseline' definition of Perl 5.

Whenever I allude to a feature like the  \K regex operator or the defined-or operator, I feel obliged to mention that is is only available beginning with Perl version 5.whatever and I make an effort to highlight its presence in a code example by including a  use 5.x; statement at the beginning, but I always assume that Perl 5 is the baseline for discussion nowadays. (Does anyone use a version 4 interpreter anymore?)

This is only idle curiosity, but can you say why you consider regex look-around assertions advanced?

Replies are listed 'Best First'.
Re^3: regex to rename last _ with ,
by dmitri (Priest) on Sep 14, 2014 at 12:22 UTC
    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.

      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1100500]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-04-24 06:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found