Commit c02037d6 authored by Miss Islington (bot)'s avatar Miss Islington (bot) Committed by Mariatta

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 610e5afd)
parent 465f3d08
...@@ -840,7 +840,7 @@ backreferences in a RE. ...@@ -840,7 +840,7 @@ backreferences in a RE.
For example, the following RE detects doubled words in a string. :: For example, the following RE detects doubled words in a string. ::
>>> p = re.compile(r'(\b\w+)\s+\1') >>> p = re.compile(r'\b(\w+)\s+\1\b')
>>> p.search('Paris in the the spring').group() >>> p.search('Paris in the the spring').group()
'the the' 'the the'
...@@ -947,9 +947,9 @@ number of the group. There's naturally a variant that uses the group name ...@@ -947,9 +947,9 @@ number of the group. There's naturally a variant that uses the group name
instead of the number. This is another Python extension: ``(?P=name)`` indicates instead of the number. This is another Python extension: ``(?P=name)`` indicates
that the contents of the group called *name* should again be matched at the that the contents of the group called *name* should again be matched at the
current point. The regular expression for finding doubled words, current point. The regular expression for finding doubled words,
``(\b\w+)\s+\1`` can also be written as ``(?P<word>\b\w+)\s+(?P=word)``:: ``\b(\w+)\s+\1\b`` can also be written as ``\b(?P<word>\w+)\s+(?P=word)\b``::
>>> p = re.compile(r'(?P<word>\b\w+)\s+(?P=word)') >>> p = re.compile(r'\b(?P<word>\w+)\s+(?P=word)\b')
>>> p.search('Paris in the the spring').group() >>> p.search('Paris in the the spring').group()
'the the' 'the the'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment