Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^3: regex not working properly

by graff (Chancellor)
on Mar 06, 2007 at 02:25 UTC ( [id://603341]=note: print w/replies, xml ) Need Help??


in reply to Re^2: regex not working properly
in thread regex not working properly

If you ever end up handling a line that looks like this:
|foo|bar|||baz|
I think you'll want a slightly more complicated regex -- something like:
s{ (?<! [^|] ) \s* (?! [^|] ) }{0}gx;
That uses negative look-behind and look-ahead assertions, so that a string of zero or more spaces will match (and be replaced by "0") if it is neither preceded nor followed by some character other than a pipe symbol. (That is, if the whitespace string is preceded or followed by something other than a pipe symbol, it won't match, and won't be replaced.)

The phrasing seems a bit obtuse, but the point is that a pipe symbol in line-initial or line-final position should probably cause a zero to be inserted, and when three or more pipes occur in sequence, you probably want zeros between all of them. Your simpler version for removing whitespace between two pipes won't handle those cases very well.

Personally, I prefer split for this sort of thing:

$row = join "|", map { s/^\s*$/0/; $_ } split( /\|/, $row, -1 );

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2024-04-24 10:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found