If you modify your code to
echo -e 'zero\none\ntwo\n\n three' | perl -0777 -pe 's{\s*(one\ntwo)\s*?( *)}{\n\n<begin block>\n\n$1\n\n<end block>\n\nx$2x}'
so you have a clear delimiter around your second match, your get:
zero
<begin block>
one
two
<end block>
xx
three
By swapping your space matching to non-greedy, you are no longer consuming the newlines preceding 'three'. You can get your expected result by using the multiline modifier (see Modifiers in perlre) combined with a line start metacharacter ^;
echo -e 'zero\none\ntwo\n\n three' | perl -0777 -pe 's{\s*(one\ntwo)\s*^( *)}{\n\n<begin block>\n\n$1\n\n<end block>\n\n$2}m'
yields
zero
<begin block>
one
two
<end block>
three
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.