Python regular expressions return true/false
Alternately use re.match() method:
Using re.match() will return either None, which evaluates to False, or match object, which will always be True.
Only if we want information about the part(s) that matched in regular expression to check out the contents of the match object.
If you really need True or False, just use bool
>>> bool(re.search(“hi”, “abcdefghijkl”))
>>> bool(re.search(“hi”, “abcdefgijkl”))
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()
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
Boolean with re?
Hey. Can you help please with the following. Using RE, I want to check a string, which is meant to be a passwords. True if it contains >= 10 chars, at least one lower letter, one capital letter and one number; else False. How would a code look like. Thanks.
e.g. two cases:
True = ‘bAse730onE4’
False = ‘saaaa90’