in reply to String Newline Question

In a regex, $ does not mean "end of string". It means "end of line", which is just before \n or the end of the string (whichever comes first). If you want to match the null string at the end of the string, use \z.

$_ = "foo\n"; /^foo$/; # true /^foo\z/; # false /^foo\n$/; # true /^foo\n\z/; # true
$ behaves like (?=\z|\n).

U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk

Replies are listed 'Best First'.
•Re: Re: String Newline Question
by merlyn (Sage) on Mar 23, 2002 at 22:26 UTC

      No, it behaves like (?=\n?\z). Yours would match any embedded newline.

      Oops, I was describing the situation for when /m (or (?m:)) is in effect, which was not the case.

      Update - Hm, even if that were the case, it would be wrong. It would behave like (?=\n\z?|\z).

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk