[英]REGEX - Differences between `^`, `$` and `\A`, `\Z`
As I know, re
proposes the following boundary matches. 据我所知,
re
提出以下边界匹配。
^
matches at the beginning of a line. ^
在一行的开头匹配。 $
matches at the end of a line. $
匹配在一行的末尾。 \\A
matches the beginning of the input. \\A
匹配输入的开头。 \\Z
matches the end of the input. \\Z
匹配输入的结尾。 Can you give me a concret example showing a real difference between between ^
, $
and \\A
, \\Z
? 你能给我一个具体的例子,显示
^
, $
和\\A
, \\Z
之间真正的区别吗?
The difference only becomes apparent when you use the re.M
or re.MULTILINE
multiline flag : 当您使用
re.M
或re.MULTILINE
多行标记时,差异才会变得明显:
>>> re.search(r'^word', 'Line one\nword on line two\n', flags=re.M)
<_sre.SRE_Match object at 0x10124f578>
>>> re.search(r'\Aword', 'Line one\nword on line two\n', flags=re.M) is None
True
where ^
matched at the start of a line (following a newline). 其中
^
在一行的开头匹配(在换行符之后)。 $
matches at the end of a line: 行尾的
$
匹配:
>>> re.search(r'word$', 'Line one word\nLine two\n', flags=re.M)
<_sre.SRE_Match object at 0x10123e1d0>
>>> re.search(r'word\Z', 'Line one word\nLine two\n', flags=re.M) is None
True
From the documentation: 从文档:
re.M
re.MULTILINE
When specified, the pattern character
'^'
matches at the beginning of the string and at the beginning of each line (immediately following each newline);指定时,模式字符
'^'
匹配字符串的开头和每行的开头(紧跟在每个换行符之后); and the pattern character'$'
matches at the end of the string and at the end of each line (immediately preceding each newline).并且模式字符
'$'
在字符串的末尾和每行的末尾(紧接在每个换行符之前)匹配。 By default,'^'
matches only at the beginning of the string, and'$'
only at the end of the string and immediately before the newline (if any) at the end of the string.默认情况下,
'^'
仅匹配字符串的开头,'$'
仅匹配字符串的末尾,紧接在字符串末尾的换行符(如果有)之前。
\\A
always matches at the start of the string regardless, \\Z
always at the end. \\A
始终匹配字符串的开头, \\Z
始终在最后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.