How you can get a true/false from a Python regular expressions?


When you use match and search methods of module re, if there is a match, it has bool value of True and if there is no match, you get None that has a bool value of False.

Match objects are always true, and None is returned if there is no match

>>> bool(re.search("def", "abcdefgh"))
True
>>> bool(re.search("rest", "pqrstuv"))
False

Updated on: 16-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements