开发者

Did $ use to sometimes match a newline in Perl?

开发者 https://www.devze.com 2023-01-11 12:46 出处:网络
I seem to remember that there were certain conditions under which $ would mat开发者_开发百科ch a newline (not \"just before\"), but I can\'t find anything in the docs that would confirm that.Did that

I seem to remember that there were certain conditions under which $ would mat开发者_开发百科ch a newline (not "just before"), but I can't find anything in the docs that would confirm that. Did that use to be true in an earlier version of Perl or am I dreaming?


That has always been the case. See perldoc perlreref ($ is an anchor):

$ Match string end (or line, if /m is used) or before newline

You should use \z if you want to match the end of the string:

\z Match absolute string end


You are confusing two very similar things. The $ matches at the end of a string if /m is not used, and at the end of a string or at the end of a string except for a final newline if /m is used. However, it does not actually match (consume) the newline itself. You could still match the newline character itself with something else in the regexp, for instance \n; or, if you also use /s, then /blah$./sm would theoretically match "blah\n" even though the $ is not the last thing in the regex.

(The reason this doesn't actually work is that both $. and $\ are actually interpreted as special variables instead of as $ plus ., so it is actually quite hard to put something after a $ in a regexp. Confusing, isn't it?)


If you pass the /s modifier to a regexp, then $ matches the end of the string and allows . to match \n.

This can be a bit confusing. For a string of foo\n, observe the difference between:

/.$/ # matches 'o'

/.$/s # still matches 'o'

/.+$/s # matches 'foo\n'

/.\z/ # doesn't match at all

/.\z/s # matches '\n'


Adding the 'm' modifier, '$' will match (but not consume) end of line characters. Normally '$' matches end of string.

my $sample = "first\nsecond\nthird";
$sample =~ s/$/END/mg;
print $sample;

Produces:

firstEND
secondEND
thirdEND

If you want to consume the newline, just use the \n escape character:

my $sample = "first\nsecond\nthird";
$sample =~ s/\n/END/g;
print $sample;

Now you get:

firstENDsecondENDthird

Note that the 'm' modifier also affects '^' in the same way as '$'.

0

精彩评论

暂无评论...
验证码 换一张
取 消