In regular expressions, named groups allow you to give a name to a capturing group in addition to its numerical index. This can make your regular expressions more readable and easier to maintain.
To define a named group in Python's re module, you use the syntax (?P<name>pattern), where name is the name you want to give to the group, and pattern is the regular expression pattern that defines the group.
For example, suppose you want to extract the first and last name from a string that contains a person's full name. You could use the following regular expression pattern:
import retext = "John Smith"pattern = r"(?P<first>\w+) (?P<last>\w+)"match = re.match(pattern, text)if match: print(match.group("first")) print(match.group("last")) |
In this example, the regular expression pattern (?P<first>\w+) (?P<last>\w+) matches two words separated by a space. The (?P<first>\w+) part captures the first word and assigns it the name "first", and the (?P<last>\w+) part captures the second word and assigns it the name "last".
The re.match() function matches the pattern against the input string text, and the match.group() method is used to extract the values of the named groups.
The output of this program is:
JohnSmith |
As you can see, the group() method can be used with the name of the named group to extract the captured value.
Named groups are especially useful when you have complex regular expressions that contain many groups, and you want to refer to them by name instead of index. They can also make your regular expressions more self-documenting, since you can give meaningful names to the groups you are capturing.