Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

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]
[chacham]: i'm reading an article on and thee is a disturbing message on the bottom right that says, "Fork me on GitHub". Maybe i just have a dirty mind.
[choroba]: Check the ninja on
[chacham]: oh my...
[Corion]: choroba++ # fixed width
marto has a Dirty_Mind on vinyl
[Corion]: Heh. The upside of this phone call was at least commendations from my cow-orkers on how clam I stayed and how I always brought the topic back to my priorities and the established processes of $work (them only getting half of the conversation).

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (9)
As of 2017-08-17 13:02 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (287 votes). Check out past polls.