Dollar sign in regular expression and new line character

Note that ^ and $ are zero-width tokens. So, they don’t match any character, but rather matches a position.

  • ^ matches the position before the first character in a string.
  • $ matches the position before the first newline in the string.

So, the String before the $ would of course not include the newline, and that is why ([A-Za-z ]+\n)$ regex of yours failed, and ([A-Za-z ]+)$\n succeeded.

In simple words, your $ should be followed by a newline, and no other character.

Leave a Comment