From a4038d8cf0f537a8d0bb5433a233532bcdbf7bbd Mon Sep 17 00:00:00 2001 From: Andrew Kuchling Date: Tue, 2 May 2023 21:55:53 -0400 Subject: [PATCH] Clarify discussion of named groups --- Doc/howto/regex.rst | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Doc/howto/regex.rst b/Doc/howto/regex.rst index c11fecb08aba8a..3394b7629be69f 100644 --- a/Doc/howto/regex.rst +++ b/Doc/howto/regex.rst @@ -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* 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 ` 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...)``. 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 ` 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\b\w+\b)') >>> m = p.search( '((( Lots of punctuation )))' )