Skip to content

Commit 610e5af

Browse files
mandeepMariatta
authored andcommitted
bpo-30004: Fix the code example of using group in Regex Howto Docs (GH-4443)
The provided code example was supposed to find repeated words, however it returned false results.
1 parent 9316ee4 commit 610e5af

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Doc/howto/regex.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ backreferences in a RE.
844844

845845
For example, the following RE detects doubled words in a string. ::
846846

847-
>>> p = re.compile(r'(\b\w+)\s+\1')
847+
>>> p = re.compile(r'\b(\w+)\s+\1\b')
848848
>>> p.search('Paris in the the spring').group()
849849
'the the'
850850

@@ -943,9 +943,9 @@ number of the group. There's naturally a variant that uses the group name
943943
instead of the number. This is another Python extension: ``(?P=name)`` indicates
944944
that the contents of the group called *name* should again be matched at the
945945
current point. The regular expression for finding doubled words,
946-
``(\b\w+)\s+\1`` can also be written as ``(?P<word>\b\w+)\s+(?P=word)``::
946+
``\b(\w+)\s+\1\b`` can also be written as ``\b(?P<word>\w+)\s+(?P=word)\b``::
947947

948-
>>> p = re.compile(r'(?P<word>\b\w+)\s+(?P=word)')
948+
>>> p = re.compile(r'\b(?P<word>\w+)\s+(?P=word)\b')
949949
>>> p.search('Paris in the the spring').group()
950950
'the the'
951951

0 commit comments

Comments
 (0)