1 """Filename matching with shell patterns.
3 fnmatch(FILENAME, PATTERN) matches according to the local convention.
4 fnmatchcase(FILENAME, PATTERN) always takes case in account.
6 The functions operate by translating the pattern into a regular
7 expression. They cache the compiled regular expressions for speed.
9 The function translate(PATTERN) returns a regular expression
10 corresponding to PATTERN. (It does not compile it.)
17 def fnmatch(name
, pat
):
18 """Test whether FILENAME matches PATTERN.
20 Patterns are Unix shell style:
23 ? matches any single character
24 [seq] matches any character in seq
25 [!seq] matches any char not in seq
27 An initial period in FILENAME is not special.
28 Both FILENAME and PATTERN are first case-normalized
29 if the operating system requires it.
30 If you don't want this, use fnmatchcase(FILENAME, PATTERN).
34 name
= os
.path
.normcase(name
)
35 pat
= os
.path
.normcase(pat
)
36 return fnmatchcase(name
, pat
)
38 def fnmatchcase(name
, pat
):
39 """Test wheter FILENAME matches PATTERN, including case.
41 This is a version of fnmatch() which doesn't case-normalize
45 if not _cache
.has_key(pat
):
47 _cache
[pat
] = re
.compile(res
)
48 return _cache
[pat
].match(name
) is not None
51 """Translate a shell PATTERN to a regular expression.
53 There is no way to quote meta-characters.
67 if j
< n
and pat
[j
] == '!':
69 if j
< n
and pat
[j
] == ']':
71 while j
< n
and pat
[j
] != ']':
79 stuff
= '[^' + stuff
[1:] + ']'
80 elif stuff
== '^'*len(stuff
):
83 while stuff
[0] == '^':
84 stuff
= stuff
[1:] + stuff
[0]
85 stuff
= '[' + stuff
+ ']'
88 res
= res
+ re
.escape(c
)