Under the Covers¶
wrap Python’s native``_sre.SRE_Match`` objects (the things that
method calls return).:
match = re.match(r'(?P<word>th.s)', 'this is a string') match = ReMatch(match) if match: print match.group(1) # still works print match # same thing print match.word # same thing, with logical name
But that’s a lot of boilerplate for a simple test, right? So
en passant operator redefining the division operation and proxies the
on the fly to the pre-defined
if match / re.search(r'(?P<word>th.s)', 'this is a string'): assert match == 'this' assert match.word == 'this' assert match.group(1) == 'this'
re operation fails, the resulting object is guaranteed to have
False-like Boolean value, so that it will fall through conditional tests.