Your regular expression contains a negative lookahead which specifically rejects any match where the matched string "Prompt: "
is followed by a newline.
With [ \t]?
there is a way to find a match by not matching the space, so the regex engine chooses that, in its desperate quest to return a match if there is a way to produce one. With [ \t]+
you don't offer a way out, so no match can be found.
It's not entirely clear why you put the assertion there; but removing it certainly allows the string to match as expected and apparently required.
It doesn't really matter here, but common practice is to use raw Python strings r'...'
for regular expressions. In your example, having Python replace \t
with a literal tab and \n
with a literal newline is weird but technically harmless, since those are the actual characters you want to match (and maybe not match, respectively??) but breaks completely with many other backslashed sequences like \s
and \d
.
To say "there may be whitespace but it cannot be followed by a newline", try someting like
re.compile(r'Prompt:(?![ \t]*\n)')
If you want the space(s) to be included in the match, you can put \s*
after the assertion.