Skip to content

Commit

Permalink
Clarify discussion of named groups
Browse files Browse the repository at this point in the history
  • Loading branch information
akuchling committed Aug 9, 2023
1 parent 1eaa7ac commit a4038d8
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions Doc/howto/regex.rst
Original file line number Diff line number Diff line change
Expand Up @@ -950,12 +950,14 @@ A more significant feature is named groups: instead of referring to them by
numbers, groups can be referenced by a name.

The syntax for a named group is one of the Python-specific extensions:
``(?P<name>...)``. *name* can be used to refer to the group in other contexts. Named groups
behave exactly like capturing groups, and additionally associate a name
with a group. The :ref:`match object <match-objects>` methods that deal with
capturing groups all accept either integers that refer to the group by number
or strings that contain the desired group's name. Named groups are still
given numbers, so you can retrieve information about a group in two ways::
``(?P<name>...)``. Named groups behave exactly like capturing groups, and
additionally associate *name* with the group so that *name* can be used to
refer to the group in other contexts. Names should look like a Python
identifier andonly contain letters, digits and underscores. The :ref:`match
object <match-objects>` methods that deal with capturing groups all accept
either integers that refer to the group by number or strings that contain the
desired group's name. Named groups are still given numbers, so you can
retrieve information about a group in two ways::

>>> p = re.compile(r'(?P<word>\b\w+\b)')
>>> m = p.search( '((( Lots of punctuation )))' )
Expand Down

0 comments on commit a4038d8

Please sign in to comment.