Alternative to the `match = re.match(); if match: ...` idiom? Alternative to the `match = re.match(); if match: ...` idiom? python python

Alternative to the `match = re.match(); if match: ...` idiom?


I don't think it's trivial. I don't want to have to sprinkle a redundant conditional around my code if I'm writing code like that often.

This is slightly odd, but you can do this with an iterator:

import redef rematch(pattern, inp):    matcher = re.compile(pattern)    matches = matcher.match(inp)    if matches:        yield matchesif __name__ == '__main__':    for m in rematch("(\d+)g", "123g"):        print(m.group(1))

The odd thing is that it's using an iterator for something that isn't iterating--it's closer to a conditional, and at first glance it might look like it's going to yield multiple results for each match.

It does seem odd that a context manager can't cause its managed function to be skipped entirely; while that's not explicitly one of the use cases of "with", it seems like a natural extension.


Another nice syntax would be something like this:

header = re.compile('(.*?) = (.*?)$')footer = re.compile('(.*?): (.*?)$')if header.match(line) as m:    key, value = m.group(1,2)elif footer.match(line) as m    key, value = m.group(1,2)else:    key, value = None, None


Starting Python 3.8, and the introduction of assignment expressions (PEP 572) (:= operator), we can now capture the condition value re.match(r'(\d+)g', '123g') in a variable match in order to both check if it's not None and then re-use it within the body of the condition:

>>> if match := re.match(r'(\d+)g', '123g'):...   print(match.group(1))... 123>>> if match := re.match(r'(\d+)g', 'dddf'):...   print(match.group(1))...>>>