Python regular expressions return true/false
Match
objects are always true, and None
is returned if there is no match. Just test for trueness.
if re.match(...):
If you really need True
or False
, just use bool
>>> bool(re.search("hi", "abcdefghijkl"))
True
>>> bool(re.search("hi", "abcdefgijkl"))
False
As other answers have pointed out, if you are just using it as a condition for an if
or while
, you can use it directly without wrapping in bool()