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: zsh-style subpattern matching for fnmatch/glob
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.10
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: erickt, georg.brandl, giampaolo.rodola, ironfroggy, kveretennicov, rhettinger, terry.reedy, tim.golden
Priority: normal Keywords: patch

Created on 2008-12-07 08:17 by erickt, last changed 2022-04-11 14:56 by admin.

Files
File name Uploaded Description Edit
zsh-fnmatch.diff erickt, 2008-12-07 08:17
zsh-fnmatch-2.7.diff ironfroggy, 2008-12-08 00:03 Backport of original patch for 2.7
Messages (7)
msg77216 - (view) Author: Erick Tryzelaar (erickt) Date: 2008-12-07 08:17
As I mentioned on python-ideas, I my project needs to extend fnmatch to 
support zsh-style globbing, where you can use brackets to designate 
subexpressions. Say you had a directory structure like this:

foo/
 foo.ext1
 foo.ext2
bar/
 foo.ext1
 foo.ext2

The subexpressions will let you do patterns like this:

>>> glob.glob('foo/foo.{ext1,ext2}')
['foo/foo.ext1', 'foo/foo.ext2']
>>> glob.glob('foo/foo.ext{1,2}')
['foo/foo.ext1', 'foo/foo.ext2']
>>> glob.glob('{foo,bar}')
['bar', 'foo']
>>> glob.glob('{foo,bar}/foo*')
['bar/foo.ext1', 'bar/foo.ext2', 'foo/foo.ext1', 'foo/foo.ext2']
>>> glob.glob('{foo,bar}/foo.{ext*}')
['bar/foo.ext1', 'bar/foo.ext2', 'foo/foo.ext1', 'foo/foo.ext2']
>>> glob.glob('{f?o,b?r}/foo.{ext*}')
['bar/foo.ext1', 'bar/foo.ext2', 'foo/foo.ext1', 'foo/foo.ext2']


Would this be interesting to anyone else? It would unfortunately break 
fnmatch since it currently treats {} as ordinary characters. It'd be 
easy to work around that by adding a flag or using a different function 
name though. Anyway, here's the patch against the head of py3k.
msg77278 - (view) Author: Calvin Spealman (ironfroggy) Date: 2008-12-08 00:03
This should be applicable to 2.7, at least, as well. Here is a backport 
of the patch against trunk.
msg85073 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2009-04-01 18:51
This can't go in like this, since suddenly accepting braces is a subtle
change of semantics.  I could imagine adding another function though,
that has extended zsh-like globbing abilities.
msg85130 - (view) Author: Erick Tryzelaar (erickt) Date: 2009-04-02 01:31
I completely agree, Georg. I tried to get this in before 3.0, but it
didn't work out which is a shame. I assume you also feel that we
couldn't make a backwards compatible change in 3.1, right? I thought I
heard that 3.1 may break some things in 3.0, but I'm not positive.

Got any suggestions for a function name? Here are some I have in mind,
but none of them are particularly good:

fnmatch.fnmatch2
fnmatch.efnmatch
fnmatch.extended_fnmatch

glob.glob2
glob.eglob
glob.extended_glob
msg85131 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-04-02 01:34
I like glob_ext() and fnmatch_ext().
msg85180 - (view) Author: Tim Golden (tim.golden) * (Python committer) Date: 2009-04-02 07:42
Is there mileage for glob.glob to grow a dialect
param, with a default value to keep it backwards
compatible? Otherwise, presumably, proponents of
some other xsh variant will come forward with
their scheme of matching, and regex-followers
with theirs and so on.
msg379369 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2020-10-22 21:52
Raymond, is this zsh addition still relevant or out of date?
History
Date User Action Args
2022-04-11 14:56:42adminsetgithub: 48823
2020-10-22 22:05:35terry.reedysetversions: + Python 3.10, - Python 3.0, Python 2.7
2020-10-22 21:52:47terry.reedysetnosy: + terry.reedy
messages: + msg379369
2010-09-27 20:40:12kveretennicovsetnosy: + kveretennicov
2009-09-04 06:52:18georg.brandllinkissue2649 superseder
2009-04-02 07:42:03tim.goldensetnosy: + tim.golden
messages: + msg85180
2009-04-02 01:34:59rhettingersetnosy: + rhettinger
messages: + msg85131
2009-04-02 01:31:01ericktsetmessages: + msg85130
2009-04-01 18:51:26georg.brandlsetnosy: + georg.brandl
messages: + msg85073
2008-12-08 00:04:08ironfroggysetversions: + Python 2.7
2008-12-08 00:03:56ironfroggysetfiles: + zsh-fnmatch-2.7.diff
nosy: + ironfroggy
messages: + msg77278
2008-12-07 14:20:56giampaolo.rodolasetnosy: + giampaolo.rodola
2008-12-07 08:17:56ericktcreate