Regex - match returns None. Where am I wrong?

Your regex does not match the full string. You can use search instead as Useless mentioned, or you can change your regex to match the full string:

'^this is a test$'

Or somewhat harder to read but somewhat less useless:

'^t[^t]*test$'

It depends on what you're trying to do.


Use

match1 = reg1.search(s)

instead. The match function only matches at the start of the string ... see the documentation here:

Python offers two different primitive operations based on regular expressions: re.match() checks for a match only at the beginning of the string, while re.search() checks for a match anywhere in the string (this is what Perl does by default).

Tags:

Python

Regex