且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

美元符号正则表达式和换行符

更新时间:2023-02-18 11:55:52

注意 ^$zero-width 标记.因此,它们不匹配任何字符,而是匹配 position.

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

  • ^ 匹配字符串中第一个字符之前的位置.
  • $ 匹配字符串中第一个 newline 之前的位置.
  • ^ matches the position before the first character in a string.
  • $ matches the position before the first newline in the string.

所以,$ 之前的 String 当然不包括 newline,这就是为什么 ([A-Za-z ]+\n)$ 你的正则表达式失败,([A-Za-z ]+)$\n 成功.

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.