How to extract the first non-Null match from a group of regexp matches in Python?
- by bodacydo
Suppose I have a regular expression (a)|(b)|(c)|(d). If I apply it to text 'foobar' I get a match object
>>> compiled = re.compile('(a)|(b)|(c)|(d)')
>>> compiled.search('foobar').groups()
(None, 'b', None, None)
How do I extract the 'b' from here? Or in general, how do I extract the first match from an unknown number of groups (can happen when the regexp was built dynamically)?