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 );

In reply to Re^3: regex not working properly by graff
in thread regex not working properly by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.