Issue 13929: fnmatch to support escape characters (original) (raw)

Created on 2012-02-03 06:50 by fruch, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (5)
msg152495 - (view) Author: Israel Fruchter (fruch) Date: 2012-02-03 06:50
fnmatch to support escape characters: like that: >>> name = "Document[Ver.2].doc" >>> pattern = "*\[Ver.2\]*" >>> fnmatch.fnmatch(name, pattern) True that's also fix glob module: >>> pattern = "ipconfig /\?" >>> glob.glob(pattern) "ipconfig /?"
msg152590 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-02-04 04:41
The doc chapters are entitled "fnmatch — Unix filename pattern matching" and "glob — Unix style pathname pattern expansion". The first explicitly disclaims the feature you request: "Be aware there is no way to quote meta-characters.", suggests using re for anything beyond fnmatch, and shows to use .translate to make a start in doing so. For your example: >>> re.match(r".*\[Ver\.2\].*", "Document[Ver.2].doc") <_sre.SRE_Match object at 0x000000000331AF38> Indeed, fnmatch works by using translate() and re.match. What you are asking for in something in between the unix language and re. If one re feature is added, why not another? So the scope of these modules is clearly circumscribed. I suspect their intent was to make it easy to translate unix shell scripts into Python. What you are asking for in something in between. If you want to pursue this, post on python-list or python-ideas to garner more support. But I anticipate rejection as not needed and contrary to intent. Not obvious from the doc is that an unmatch '[' or ']' is escaped: >>> name = "Document[Ver.2.doc" >>> pattern = "*[Ver.2*" >>> fnmatch.fnmatch(name, pattern) True >>> name = "DocumentVer.2].doc" >>> pattern = "*Ver.2]*" >>> fnmatch.fnmatch(name, pattern) True I presume this matches the *nix behavior, but don't know.
msg152598 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2012-02-04 08:11
> [fnmatch] explicitly disclaims the feature you request: "Be aware there is no way to quote > meta-characters." This reads like a warning to me, i.e. a potential future feature, not a design choice. > What you are asking for in something in between the unix language and re. If one re > feature is added, why not another? When we use glob patterns in our shells, the shell language lets us escape what would otherwise be special characters. Python would be nicer to let us do the same.
msg152605 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2012-02-04 09:48
If indeed fnmatch does not match current shells, then I would agree that it should. It looks to me so easy to add that I though it must be a deliberate decision to exclude. In translate: ... elif c == '\': if i < n-1: c2 = pat[i+1] <specify what to do for \c2 for all cases of c2> else: <specify what to do for pattern ending in '\'> else: res = res + re.escape(c) # the last two lines are current code, which is why '\' in patterns does not escape anything in the translated re. Changing the meaning of '\' from ordinary character to escape char will break any code that depends on its current ordinariness. >>> fn.fnmatch(r'\x', r'\?') True # for x any 'ordinary' char, but not is '\?' means "match '?'. This was another reason I closed, although I forgot to mention it. I suppose a new parameter 'escape = False' could be added to all 4 exposed functions to preserve back compatibility. Anyway, I have reopened for further discussion and specification.
msg172962 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2012-10-15 12:02
Issue8402 has discussion and patch(es).
History
Date User Action Args
2022-04-11 14:57:26 admin set github: 58137
2012-10-15 12:02:42 serhiy.storchaka set status: open -> closednosy: + serhiy.storchakamessages: + superseder: Add a function to escape metacharacters in glob/fnmatchresolution: duplicate
2012-02-04 09:48:53 terry.reedy set status: closed -> openresolution: rejected -> (no value)messages: +
2012-02-04 08:11:27 eric.araujo set nosy: + eric.araujomessages: +
2012-02-04 04:41:11 terry.reedy set status: open -> closedversions: - Python 2.6, Python 3.1, Python 2.7, Python 3.2, Python 3.4nosy: + terry.reedymessages: + resolution: rejected
2012-02-03 06:50:23 fruch create