You've tripped over the most-voodoo of Perl's parsing. $\ is a variable and the regex parser needs to decide whether $\n meant "end-of-line then newline" or "the contents of $\ then the letter 'n'". Perl prefers the latter interpretation. Several ways to try to resolve this don't work:
perl -le'$_="ABC\nDEF"; s/C\$\n^D/-/m; print' perl -le'$_="ABC\nDEF"; s/C$[\n]^D/-/m; print'
But there are several ways to successfully work around the problem:
perl -le'$_="ABC\nDEF"; s/C$ \n^D/-/mx; print' perl -le'$_="ABC\nDEF"; s/C(?:$)\n^D/-/m; print' perl -le'$_="ABC\nDEF"; s/C$(?:\n)^D/-/m; print'
But they produce "AB-EF" not "ABC-DEF". And the following one will never match anything if it were parsed the way you expected, since there has to be a \n in the matched string between $ and ^:
perl -le'$_="ABC\nDEF"; s/C$^D/-/m; print'
- tye
In reply to Re: Why \n matches but not $^? (weight)
by tye
in thread Why \n matches but not $^?
by tel2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |