From: Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> Date: Sat, 25 Nov 2017 05:01:39 +0000 (-0800) Subject: bpo-30004: Fix the code example of using group in Regex Howto Docs (GH-4443) (GH... X-Git-Tag: v2.7.15rc1~114 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c02037d62284f4d4ca6b22f2ed05165ce2014951;p=python 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 610e5afdcbe3eca906ef32f4e0364e20e1b1ad23) --- diff --git a/Doc/howto/regex.rst b/Doc/howto/regex.rst index 0d0e9f5609..082fc01299 100644 --- a/Doc/howto/regex.rst +++ b/Doc/howto/regex.rst @@ -840,7 +840,7 @@ backreferences in a RE. 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() 'the the' @@ -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 that the contents of the group called *name* should again be matched at the current point. The regular expression for finding doubled words, -``(\b\w+)\s+\1`` can also be written as ``(?P\b\w+)\s+(?P=word)``:: +``\b(\w+)\s+\1\b`` can also be written as ``\b(?P\w+)\s+(?P=word)\b``:: - >>> p = re.compile(r'(?P\b\w+)\s+(?P=word)') + >>> p = re.compile(r'\b(?P\w+)\s+(?P=word)\b') >>> p.search('Paris in the the spring').group() 'the the'