Skip to content

Commit c02037d

Browse files
miss-islingtonMariatta
authored andcommitted
bpo-30004: Fix the code example of using group in Regex Howto Docs (GH-4443) (GH-4555)
The provided code example was supposed to find repeated words, however it returned false results. (cherry picked from commit 610e5af)
1 parent 465f3d0 commit c02037d

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
@@ -840,7 +840,7 @@ backreferences in a RE.
840840

841841
For example, the following RE detects doubled words in a string. ::
842842

843-
>>> p = re.compile(r'(\b\w+)\s+\1')
843+
>>> p = re.compile(r'\b(\w+)\s+\1\b')
844844
>>> p.search('Paris in the the spring').group()
845845
'the the'
846846

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

952-
>>> p = re.compile(r'(?P<word>\b\w+)\s+(?P=word)')
952+
>>> p = re.compile(r'\b(?P<word>\w+)\s+(?P=word)\b')
953953
>>> p.search('Paris in the the spring').group()
954954
'the the'
955955

0 commit comments

Comments
 (0)