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

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

本文介绍了美元符号正则表达式和换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道美元符号用于匹配字符串末尾的字符,以确保搜索不会在字符串中间停止,而是一直持续到字符串末尾.

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 till the end of the string.

但是它是如何处理换行符的,它是在换行符之前匹配还是考虑到这一点.

But how does it deal with the newline character, does it match just before the new line character or does it take that into account.

我在 eclipse regex 中检查了它,以获得与字符串匹配的正则表达式数组([A-Za-z ]+)$\n 有效,而不是 ([A-Za-z ]+\n)$

I checked it in eclipse regex, for a regex matching array of strings([A-Za-z ]+)$\n worked, not the other way around ([A-Za-z ]+\n)$

推荐答案

注意 ^$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.

这篇关于美元符号正则表达式和换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 08:45