问题描述
- 据说
-
^
与开头的行相匹配,但是与"\n"
,"\r"
或"\r\n"
紧接不匹配.但是,它匹配字符串的开头.从什么意义上说,它与行的开头相匹配?它与\A
有什么区别?
^
is said to match the beginning of a line, but it does not match right after a"\n"
,"\r"
or"\r\n"
. It matches the beginning of a string, though. In what sense does it match the beginning of a line, and how is it different from\A
?
$
与行的末端相匹配,但与"\n"
,"\r"
或"\r\n"
紧靠不匹配.但是,它匹配字符串的结尾.它在什么意义上与行尾匹配?它与\z
有什么区别?
$
is said to match the end of a line, but it does not match right before a "\n"
, "\r"
or "\r\n"
. It matches the end of a string, though. In what sense does it match the end of a line, and how is it different from \z
?
\Z
与\z
不同,如果"\n"
在字符串的末尾,则匹配在"\n"
之前.在我看来,\A
和\z
是自然配对的概念,而\Z
却是一个奇怪的概念.为什么\Z
和\z
是按原样定义的,而不是相反的?而且,您什么时候要使用\Z
?
\Z
, unlike \z
, matches right before "\n"
if that is at the end of a string. It seems to me that \A
and \z
are naturally paired concept, and \Z
is rather an odd one. Why is it that \Z
and \z
are defined as is, and not the other way around? And, when would you want to use \Z
?
您可以使用示例说明以上内容吗?如果语言/标准之间的差异很重要,将它们列出会很有帮助.
Can you illustrate the above using examples?If difference among languages/standards matters, it would be helpful to list them.
推荐答案
不同之处在于^
和$
锚点可以具有已修改的行为.
The difference is that the ^
and $
anchors can have modified behaviors.
启用multiline
模式后,^
和$
锚点会断言一行的开始和结束.
With multiline
mode on, the ^
and $
anchors assert the beginning and end of a line.
在multiline
模式关闭的情况下,^
和$
锚定断言字符串的开头和结尾.
With multiline
mode off, the ^
and $
anchors assert the beginning and end of the string.
大多数正则表达式实现具有multiline
模式.
Most regex implementations have a multiline
mode.
对于Ruby,Perl或Javascript,它是使用m
修饰符定义的.例如/pattern/m
With Ruby, Perl, or Javascript, it's defined with the m
modifier. e.g. /pattern/m
在.NET中,它是使用模式本身内部的(?m)
定义的,或者是通过RegexOptions.Multiline
枚举定义的.
In .NET it's defined with (?m)
inside the pattern itself, or from the RegexOptions.Multiline
enumeration.
要回答您的第三个问题...
To answer your 3rd question...
\A
-匹配项必须出现在字符串的开头.
\A
- The match must occur at the start of the string.
\Z
-匹配项必须在字符串末尾或\n
字符串末尾之前进行.
\Z
- The match must occur at the end of the string or before \n
at the end of the string.
\z
-匹配项必须出现在字符串的末尾.
\z
- The match must occur at the end of the string.
这三个常量不受任何修饰符的影响.我同意\A
和\z
似乎是不合理的配对.对我来说,这也不是很有意义.但是,如果您可能有一个尾随换行符而希望忽略的话,那么\Z
可能是首选.
These three are constants that are not affected by any modifiers. I agree that \A
and \z
seem to be an illogical pairing. It doesn't make a great deal of sense to me either. But in a case where you may have a trailing line feed that you wish to ignore then \Z
might be preferred.
这篇关于正则表达式锚清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!