[issue25517] regex howto example in "Lookahead Assertions"
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
New submission from Pavel: 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")
---------- assignee: docs@python components: Documentation messages: 253725 nosy: Pavel, docs@python priority: normal severity: normal status: open title: regex howto example in "Lookahead Assertions" type: behavior versions: Python 2.7, Python 3.2, Python 3.3, Python 3.4, Python 3.5, Python 3.6 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue25517> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Serhiy Storchaka added the comment: 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 "[^.]*". ---------- nosy: +akuchling, georg.brandl, serhiy.storchaka stage: -> needs patch versions: -Python 3.2, Python 3.3 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue25517> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Changes by Ezio Melotti <ezio.melotti@gmail.com>: ---------- components: +Regular Expressions nosy: +ezio.melotti, mrabarnett versions: -Python 3.4 _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue25517> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Elena Oat added the comment: Corrected the regular expression and documented why [^.]* is used. ---------- keywords: +patch nosy: +Elena.Oat Added file: http://bugs.python.org/file41566/issue25517.diff _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue25517> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Roundup Robot added the comment: New changeset c6b5c03183e3 by Ezio Melotti in branch '2.7': #25517: fix regex in the regex howto. Patch by Elena Oat. https://hg.python.org/cpython/rev/c6b5c03183e3 New changeset 6bd4a4907f66 by Ezio Melotti in branch '3.5': #25517: fix regex in the regex howto. Patch by Elena Oat. https://hg.python.org/cpython/rev/6bd4a4907f66 New changeset 48e2f5915d49 by Ezio Melotti in branch 'default': #25517: merge with 3.5. https://hg.python.org/cpython/rev/48e2f5915d49 ---------- nosy: +python-dev _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue25517> _______________________________________
![](https://secure.gravatar.com/avatar/fa0f7819f1825f596b384c19aa7dcf33.jpg?s=120&d=mm&r=g)
Ezio Melotti added the comment: Fixed, thanks for the patch! ---------- resolution: -> fixed stage: needs patch -> resolved status: open -> closed _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue25517> _______________________________________
participants (5)
-
Elena Oat
-
Ezio Melotti
-
Pavel
-
Roundup Robot
-
Serhiy Storchaka