How to extract the first non-Null match from a group of regexp matches in Python?
Posted
by bodacydo
on Stack Overflow
See other posts from Stack Overflow
or by bodacydo
Published on 2010-03-23T10:05:06Z
Indexed on
2010/03/23
10:13 UTC
Read the original article
Hit count: 473
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)?
© Stack Overflow or respective owner