Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Re: regex not working properly

by JediWizard (Deacon)
on Mar 05, 2007 at 21:16 UTC ( #603301=note: print w/replies, xml ) Need Help??

in reply to regex not working properly

$row =~ s#\s+$##; looks to be the problem. the $ means it will only match at the end of the string. if you want to remove all spaces:

$row =~ s/\s+//g; # Remove only spaces in other wise empty records $row =~ s/\|\s+\|/||/g; # Same but add a zero even for empty items $row =~ s/\|\s*\|/|0|/g;
(code is untested)

They say that time changes things, but you actually have to change them yourself.

—Andy Warhol

Replies are listed 'Best First'.
Re^2: regex not working properly
by Anonymous Monk on Mar 05, 2007 at 22:43 UTC
    $row =~ s/\|\s+\|/||/g;
    works perfect. Thank you!
      If you ever end up handling a line that looks like this:
      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?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2022-01-17 01:10 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (50 votes). Check out past polls.