qed
qed

Reputation: 23094

Named non-capturing group in python?

Is it possible to have named non-capturing group in python? For example I want to match string in this pattern (including the quotes):

"a=b" 'bird=angel'

I can do the following:

s = '"bird=angel"'
myre = re.compile(r'(?P<quote>[\'"])(\w+)=(\w+)(?P=quote)')
m = myre.search(s)
m.groups()
# ('"', 'bird', 'angel')

The result captures the quote group, which is not desirable here.

Upvotes: 7

Views: 4094

Answers (2)

Eric O. Lebigot
Eric O. Lebigot

Reputation: 94475

You do need a capturing group in order to match the same quote: there is no other mechanism in re that allows you to do this, short of explicitly distinguishing the two quotes:

myre = re.compile('"{0}"' "|'{0}'" .format('(\w+)=(\w+)'))

(which has the downside of giving you four groups, two for each style of quotes).

Note that one does not need to give a name to the quotes, though:

myre = re.compile(r'([\'"])(\w+)=(\w+)\1')  

works as well.

In conclusion, you are better off using groups()[1:] in order to get only what you need, if at all possible.

Upvotes: 3

Bakuriu
Bakuriu

Reputation: 101909

No, named groups are always capturing groups. From the documentation of the re module:

Extensions usually do not create a new group; (?P<name>...) is the only exception to this rule.

And regarding the named group extension:

Similar to regular parentheses, but the substring matched by the group is accessible within the rest of the regular expression via the symbolic group name name

Where regular parentheses means (...), in contrast with (?:...).

Upvotes: 11

Related Questions