in reply to matching and mysterious captures
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: matching and mysterious captures
by Allasso (Monk) on May 05, 2011 at 17:15 UTC | |
by kennethk (Abbot) on May 05, 2011 at 18:00 UTC | |
by Allasso (Monk) on May 05, 2011 at 18:51 UTC |