Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Re: split on zero-length pattern

by tinita (Parson)
on Nov 26, 2010 at 10:59 UTC ( #873832=note: print w/replies, xml ) Need Help??

in reply to split on zero-length pattern

perl -lwe 'use strict; print(join("/",split(/(?!\.\d)/,"123.456.78.1")))'
The idea is that a split point is any point in the string which is preceeded by a dot followed by a digit.
In your code you have a negative look-ahead assertion (?!\.\d).
In the explanation below you talk about "a point in the string which is preceeded by". For something like that you need a positive look-behind assertion.
Maybe you just mixed them up? Using (?<=\.\d) works as expected for me.

Replies are listed 'Best First'.
Re^2: split on zero-length pattern
by rovf (Priest) on Nov 26, 2010 at 11:16 UTC
    Hmmm... a negative look-ahead looks backwards, isn't it? So I thought it should be the correct one. For example, in the string '12345.6789.0', the first split point should be after the 6, i.e. giving 1245.6 as first element. Hence my idea goes like this: To the *left* of the split point must be a period, followed by a digit. The regexp engine needs to look back, so I thought it is negative look-ahead. Did I misunderstand here the explanations in perlre?

    Ronald Fischer <>
      You are searching for "something preceeded by".
      First, that is something *positive*. Why do you want to use a a negative look-around? You are searching for something that is preceeded by, not for something that is *not* preceeded by.
      Second, a look-ahead looks *ahead* for the pattern specified (update: maybe better put: looks if the specified pattern is ahead of the look-ahead assertion). For every look-around in perlre there is a short example given. The example for negative look-behindahead is /foo(?!bar)/, saying "match a foo that is *not* *followed* by "bar". So in your code you said effectively "match anything that is not followed by a dot and a digit".

        Now I got it!!!! Thanks a lot for your patient explanation!

        Ronald Fischer <>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://873832]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2018-05-27 01:55 GMT
Find Nodes?
    Voting Booth?