To expand on
imp's suggestion with look-behind assertions, you could use look-behind and look-ahead assertions in combination so that you find a point that is preceded by a pipe symbol or the beginning of the line and followed by a pipe symbol or the newline and do the substitution there. Look-behinds have to be fixed width, hence the alternation of two of them. I have set up a variable
$litPipe to hold a literal pipe symbol to avoid a lot of escaping inside the regular expression.
use strict;
use warnings;
my $litPipe = q{\|};
my $rxBetween = qr
{(?x)
(?:
(?<=\A)
|
(?<=$litPipe)
)
(?=$litPipe|\n)
};
while (<DATA>)
{
s{$rxBetween}{EMPTY}g;
print;
}
__END__
a|b|c|d|e
f||h|i|j
|l|m|n|o
||||t
u|v|w|x|
Here's the output
a|b|c|d|e
f|EMPTY|h|i|j
EMPTY|l|m|n|o
EMPTY|EMPTY|EMPTY|EMPTY|t
u|v|w|x|EMPTY
Because the regular expression pins down exactly where you want to do the substitution it copes well with beginning and end of line situations.
I hope this is of use.
Cheers,
JohnGG
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.