Title: regex howto example in "Lookahead Assertions"
Type: behavior Stage: resolved
Components: Documentation, Regular Expressions Versions: Python 3.6, Python 3.5, Python 2.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Elena.Oat, Pavel, akuchling, docs@python, ezio.melotti, georg.brandl, mrabarnett, python-dev, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2015-10-30 10:07 by Pavel, last changed 2016-01-11 22:12 by ezio.melotti. This issue is now closed.

File name Uploaded Description Edit
issue25517.diff Elena.Oat, 2016-01-10 12:09 review
Messages (5)
msg253725 - (view) Author: Pavel (Pavel) Date: 2015-10-30 10:07
The example advises ".*[.](?!bat$).*$" expression "to match filenames where the extension is not bat". But here is an example which passes such check:

>>> re.match("(.*)[.](?!bat$).*$", "test.a.bat")
<_sre.SRE_Match object at 0x7ff221996f30>

To my mind use of negative lookbehind expressions (not covered so far in the HOWTO) is better:

>>> re.match("(.*)[.].*(?<!.bat)$", "test.a.bat")
msg253726 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2015-10-30 11:07
Note that lookbehind assertions still are not documented in Regular Expression HOWTO.

The example that demonstrates negative lookahead assertion is ".*[.](?!bat$)[^.]*$". But then we should explain why ".*" is changed to "[^.]*".
msg257906 - (view) Author: Elena Oat (Elena.Oat) * Date: 2016-01-10 12:09
Corrected the regular expression and documented why [^.]* is used.
msg258032 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2016-01-11 22:11
New changeset c6b5c03183e3 by Ezio Melotti in branch '2.7':
#25517: fix regex in the regex howto.  Patch by Elena Oat.

New changeset 6bd4a4907f66 by Ezio Melotti in branch '3.5':
#25517: fix regex in the regex howto.  Patch by Elena Oat.

New changeset 48e2f5915d49 by Ezio Melotti in branch 'default':
#25517: merge with 3.5.
msg258033 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2016-01-11 22:12
Fixed, thanks for the patch!
Date User Action Args
2016-01-11 22:12:26ezio.melottisetstatus: open -> closed
resolution: fixed
messages: + msg258033

stage: needs patch -> resolved
2016-01-11 22:11:41python-devsetnosy: + python-dev
messages: + msg258032
2016-01-10 12:09:29Elena.Oatsetfiles: + issue25517.diff

nosy: + Elena.Oat
messages: + msg257906

keywords: + patch
2016-01-04 03:42:46ezio.melottisetnosy: + ezio.melotti, mrabarnett

components: + Regular Expressions
versions: - Python 3.4
2015-10-30 11:07:05serhiy.storchakasetversions: - Python 3.2, Python 3.3
nosy: + akuchling, georg.brandl, serhiy.storchaka

messages: + msg253726

stage: needs patch
2015-10-30 10:07:26Pavelcreate