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:\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
inPython
\Z
matches only at the very end of the string. Python does not support\z
.You can use
negative lookaheads
for checking if it contains a new line character or not.In your case,^aaaa(?!\n)$
.