What the first snippet of code does (i.e.
$val =~ s/^(.*)\n/$1/; ) is capture from the beginning everything up to the first "\n" (as you mentioned . does not match \n without the /s modifier on the regex), and puts it in $1. To complete the match it also matches the "\n". The thing that you are missing is that it does not do anything to the rest of the string, but substitute the part that was matched with whatever is in the replacement part (i.e. $var =~ s/match/replace/; will replace the first instance of match in $var and replace it with replace.)
To put it another way, if you had the string $val = "dogs and cats\nfoo and bar\ntime and taxes" then it would capture "dogs and cats" in $1 and then also capture the \n after it for the match. After which it would replace just this string with "dogs and cats" resulting in a string:"dogs and catsfoo and bar\ntime and taxes" The second snippet matches because you are capturing everything after the first "\n" (inclusive) and then replacing it with nothing (effectively getting rid of the first \n and everything after it, which is what I think you are after).
-enlil
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.