This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: literal re fails to match
Type: behavior Stage: resolved
Components: Regular Expressions Versions: Python 3.3, Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Jonathan.Epstein, eric.smith, ezio.melotti, mrabarnett, yselivanov
Priority: normal Keywords:

Created on 2014-02-28 19:06 by Jonathan.Epstein, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg212463 - (view) Author: Jonathan Epstein (Jonathan.Epstein) Date: 2014-02-28 19:06
All 3 of these regex's should match, but in practice only m2 does.  Found deep in the bowels of debugging a larger problem.


import re

mainProjectsPath = '/groups/larvalolympiad/larvalolympiad/Projects/'

m1 = re.match('larvalolympiad',mainProjectsPath)
m2 = re.match('.*larvalolympiad.*',mainProjectsPath)
m3 = re.match('/larvalolympiad/',mainProjectsPath)

print(m1)
print(m2)
print(m3)
msg212468 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2014-02-28 19:23
re.match requires a match at the beginning of the string. From the docs: "If zero or more characters at the beginning of string match the regular expression pattern, ...".

If you switch to re.search, they'll all match:

>>> re.search('larvalolympiad',mainProjectsPath)
<_sre.SRE_Match object at 0xffed54f0>

>>> re.match('.*larvalolympiad.*',mainProjectsPath)
<_sre.SRE_Match object at 0xffed5870>

>>> re.search('/larvalolympiad/',mainProjectsPath)
<_sre.SRE_Match object at 0xffed54f0>
msg212469 - (view) Author: Jonathan Epstein (Jonathan.Epstein) Date: 2014-02-28 19:24
Sorry, this was dumb.  Thanks for your patience.

On Fri, Feb 28, 2014 at 2:23 PM, Eric V. Smith <report@bugs.python.org>wrote:

>
> Eric V. Smith added the comment:
>
> re.match requires a match at the beginning of the string. From the docs:
> "If zero or more characters at the beginning of string match the regular
> expression pattern, ...".
>
> If you switch to re.search, they'll all match:
>
> >>> re.search('larvalolympiad',mainProjectsPath)
> <_sre.SRE_Match object at 0xffed54f0>
>
> >>> re.match('.*larvalolympiad.*',mainProjectsPath)
> <_sre.SRE_Match object at 0xffed5870>
>
> >>> re.search('/larvalolympiad/',mainProjectsPath)
> <_sre.SRE_Match object at 0xffed54f0>
>
> ----------
> nosy: +eric.smith
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue20810>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:57:59adminsetgithub: 65009
2014-03-01 01:55:29ezio.melottisetstage: resolved
2014-02-28 19:24:21Jonathan.Epsteinsetmessages: + msg212469
2014-02-28 19:24:01eric.smithsetstatus: open -> closed
resolution: not a bug
2014-02-28 19:23:39eric.smithsetnosy: + eric.smith
messages: + msg212468
2014-02-28 19:09:34yselivanovsetnosy: + yselivanov
2014-02-28 19:06:48Jonathan.Epsteincreate