What happens with this string?
$_=qq!\nJ\nu\ns\t another Perl hacker!;
Your regexp fails, because \n is not matched by /./
Nobody have said that the two first characters matches with /./
Try this
s/(\w|\W){2}//;
Regards
Hopes | [reply] [d/l] [select] |
Very well spotted, Hopes. Dot not matching a newline is such a common mistake it's easy to forget. I'm glad that you noticed it.
<pedant>
Actually, it's possible to get dot to match everything by using the /s switch. This also has the benefit that ^ and $ will match the beginning and end of the string, and not at line breaks, which for our purposes here is desirable. See the perlre man page for details.
Hence $string =~ s/^..//s does what we want, even if we are dealing with newlines. If you don't mind losing clarity, you can even drop the ^ in this situation.
</pedant>
Having said all that, the substr method described above would be my recommendation. (It's also likely to be the fastest). ;)
Cheers,
Paul
| [reply] |
| [reply] |
s/^.{2}//s;
The s modifier at the end takes care of \n chars as well.
But substr() is the best way anyway... :)
.02
cLive ;-) | [reply] [d/l] |