in reply to Why \n matches but not $^?
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Why \n matches but not $^? (weight)
by moritz (Cardinal) on Oct 13, 2008 at 09:06 UTC | |
by tye (Sage) on Oct 14, 2008 at 00:02 UTC | |
by tel2 (Pilgrim) on Oct 13, 2008 at 22:53 UTC | |
by moritz (Cardinal) on Oct 14, 2008 at 05:57 UTC | |
|
Re^2: Why \n matches but not $^? (weight)
by tel2 (Pilgrim) on Oct 13, 2008 at 08:52 UTC | |
by ikegami (Patriarch) on Oct 13, 2008 at 13:56 UTC | |
by tye (Sage) on Oct 14, 2008 at 00:14 UTC | |
|
Re^2: Why \n matches but not $^? (weight)
by procura (Beadle) on Oct 13, 2008 at 21:32 UTC |