how can I check if group member exist ?

Hans hansyin at gmail.com
Fri Jun 21 05:07:59 EDT 2013


Hi,

I'm doing a regular expression matching, let's say "a=re.search(re_str,match_str)", if matching, I don't know how many str/item will be extracted from re_str, maybe a.group(1), a.group(2) exist but a.group(3) does not. 

Can I somehow check it? something like:
if exist(a.group(1)): print a.group(1)
if exist(a.group(2)): print a.group(2)
if exist(a.group(3)): print a.group(3)


I don't want to be hit by "Indexerror":
>>>print a.group(3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: no such group
>>> 

thanks!!!



More information about the Python-list mailing list