Python Regex-使用换行符拒绝字符串 [英] Python Regex - Reject strings with newline
问题描述
我想将完整的字符串匹配到特定的模式.假设:
word = "aaaa"
test = re.match(r"^aaaa$", word) # this returns True
但是,如果单词后跟换行符:
word = "aaaa\n"
test = re.match(r"^aaaa$", word) # Also returns True :(
但是我想找到一种在最后一种情况下返回False的方法.有没有办法区分"\ n"?
代替锚点^
和$
使用\A
作为开始,使用\Z
作为结束:
>>> print re.match(r'\Aaaaa\Z', 'aaaa')
<_sre.SRE_Match object at 0x1014b9bf8>
>>> print re.match(r'\Aaaaa\Z', 'aaaa\n')
None
\A
与字符串的实际开始匹配,而\Z
与实际的结束匹配,并且多行字符串中只能包含\A
和\Z
之一,而$
可能在每行中都匹配. /p>
与.NET
,Java
,PCRE
,Delphi
,PHP
和Python
\Z
中的PHP
不同,fyi仅在字符串的末尾匹配. Python不支持\z
.
I want to match complete strings to a specific pattern. Let's say :
word = "aaaa"
test = re.match(r"^aaaa$", word) # this returns True
However, if the word is followed by a newline character :
word = "aaaa\n"
test = re.match(r"^aaaa$", word) # Also returns True :(
But I want to find a way for it to return False in this last case. Is there a way to differentiate "\n"?
Instead of anchors ^
and $
use \A
for start and \Z
for end:
>>> print re.match(r'\Aaaaa\Z', 'aaaa')
<_sre.SRE_Match object at 0x1014b9bf8>
>>> print re.match(r'\Aaaaa\Z', 'aaaa\n')
None
\A
matches the actual start of string and \Z
the actual end and there can be only one of \A
and \Z
in a multiline string, whereas $
may be matched in each line.
I suggest reading this very good article on permanent line anchors.
Just fyi unlike .NET
, Java
, PCRE
, Delphi
, PHP
in Python
\Z
matches only at the very end of the string. Python does not support \z
.
这篇关于Python Regex-使用换行符拒绝字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!