Reputation:
Let's consider the string r"(?P<DEF_FUNC>def (?P<NAME_FUNC>\w+)\s*\((.*?)\):)|(?P<OTHERS>\w+)"
which be used later for defining a pattern.
I would like to find in this string the names used for defining the groups from the regex's point of view. Here, this names are DEF_FUNC
, NAME_FUNC
and OTHERS
.
How can I achieve that by taking care of escaping issues ?
Upvotes: 1
Views: 53
Reputation: 353139
You can find them in the groupindex
dictionary (see docs here):
>>> import re
>>> rstr = r"(?P<DEF_FUNC>def (?P<NAME_FUNC>\w+)\s*\((.*?)\):)|(?P<OTHERS>\w+)"
>>> regex = re.compile(rstr)
>>> regex.groupindex
{'DEF_FUNC': 1, 'OTHERS': 4, 'NAME_FUNC': 2}
Doing the usual tricks if you want them in value order:
>>> sorted(regex.groupindex, key=regex.groupindex.get)
['DEF_FUNC', 'NAME_FUNC', 'OTHERS']
Upvotes: 2