Dollar sign in regular expression and new line character

后端 未结 2 1737
闹比i
闹比i 2020-12-09 04:33

I know that the dollar sign is used to match the character at the end of the string, to make sure that search does not stop in the middle of the string but instead goes on t

2条回答
  •  臣服心动
    2020-12-09 05:12

    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.

提交回复
热议问题