Python Regex - Reject strings with newline

依然范特西╮ 提交于 2019-12-20 02:56:38

问题


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"?


回答1:


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.




回答2:


You can use negative lookaheads for checking if it contains a new line character or not.In your case, ^aaaa(?!\n)$.



来源:https://stackoverflow.com/questions/36696895/python-regex-reject-strings-with-newline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!