How can I match at the beginning of any line, including the first, with a Perl regex?
Posted
by JoelFan
on Stack Overflow
See other posts from Stack Overflow
or by JoelFan
Published on 2010-03-12T19:42:11Z
Indexed on
2010/03/17
3:21 UTC
Read the original article
Hit count: 300
According the the Perl documentation on regexes:
By default, the "^" character is guaranteed to match only the beginning of the string ... Embedded newlines will not be matched by "^" ... You may, however, wish to treat a string as a multi-line buffer, such that the "^" will match after any newline within the string ... you can do this by using the /m modifier on the pattern match operator.
The "after any newline" part means that it will only match at the beginning of the 2nd and subsequent lines. What if I want to match at the beginning of any line (1st, 2nd, etc.)?
EDIT: OK, it seems that the file has BOM information (3 chars) at the beginning and that's what's messing me up. Any way to get ^ to match anyway?
EDIT: So in the end it works (as long as there's no BOM), but now it seems that the Perl documentation is wrong, since it says "after any newline"
© Stack Overflow or respective owner