Match objects are always true, and None returned if there is no match. Just experience the truth.
the code:
>>> st = 'bar' >>> m = re.match(r"ba[r|z|d]",st) >>> if m: ... m.group(0) ... 'bar'
Output = bar
If you want search functionality
>>> st = "bar" >>> m = re.search(r"ba[r|z|d]",st) >>> if m is not None: ... m.group(0) ... 'bar'
and if regexp not found than
>>> st = "hello" >>> m = re.search(r"ba[r|z|d]",st) >>> if m: ... m.group(0) ... else: ... print "no match" ... no match
As @bukzor said, if st = foo bar , than a match will not work. Thus, it is more appropriate to use re.search .
RanRag Jan 25 2018-12-12T00: 00Z
source share