[docs] [issue30004] in regex-howto, improve example on grouping

Cristian Barbarosie report at bugs.python.org
Thu Apr 6 00:40:42 EDT 2017


New submission from Cristian Barbarosie:

In the Regular Expression HOWTO
https://docs.python.org/3.6/howto/regex.html#regex-howto
the last example in the "Grouping" section has a bug. The code is supposed to find repeated words, but it catches false repetitions.

>>> p = re.compile(r'(\b\w+)\s+\1')
>>> p.search('Paris in the the spring').group()
'the the'
>>> p.search('k is the thermal coefficient').group()
'the the'

I propose adding a \b after \1, this solves the problem :

>>> p = re.compile(r'(\b\w+)\s+\1\b')
>>> p.search('Paris in the the spring').group()
'the the'
>>> print p.search('k is the thermal coefficient')
None

----------
assignee: docs at python
components: Documentation
messages: 291209
nosy: Cristian Barbarosie, docs at python
priority: normal
severity: normal
status: open
title: in regex-howto, improve example on grouping
type: enhancement
versions: Python 2.7, Python 3.3, Python 3.4, Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30004>
_______________________________________


More information about the docs mailing list