Named groups
Remember from the previous chapter when we got a group through an index?
>>>pattern = re.compile(r"(\w+) (\w+)") >>>match = pattern.search("Hello⇢world") >>>match.group(1) 'Hello' >>>match.group(2) 'world'
We just learnt how to access the groups using indexes to extract information and to use it as backreferences. Using numbers to refer to groups can be tedious and confusing, and the worst thing is that it doesn't allow you to give meaning or context to the group. That's why we have named groups.
Imagine a regex in which you have several backreferences, let's say 10, and you find out that the third one is invalid, so you remove it from the regex. That means you have to change the index for every backreference starting from that one onwards. In order to solve this problem, in 1997, Guido Van Rossum designed named groups for Python 1.5. This feature was offered to Perl for cross-pollination.
Nowadays, it can be found in almost any flavor. Basically...