Python Regex-使用换行符拒绝字符串 [英] Python Regex - Reject strings with newline

查看:67
本文介绍了Python Regex-使用换行符拒绝字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将完整的字符串匹配到特定的模式.假设:

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>

我建议阅读有关永久行锚的非常好的文章.

.NETJavaPCREDelphiPHPPython \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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆