Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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.


Comment on Re: split on zero-length pattern
Select or Download Code
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 <ynnor@mm.st>
      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 <ynnor@mm.st>

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (13)
As of 2014-12-26 18:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (174 votes), past polls