Issue 4573: zsh-style subpattern matching for fnmatch/glob (original) (raw)

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.

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

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.