Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 1 | """Filename matching with shell patterns. |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 3 | fnmatch(FILENAME, PATTERN) matches according to the local convention. |
| 4 | fnmatchcase(FILENAME, PATTERN) always takes case in account. |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 6 | The functions operate by translating the pattern into a regular |
| 7 | expression. They cache the compiled regular expressions for speed. |
| 8 | |
| 9 | The function translate(PATTERN) returns a regular expression |
| 10 | corresponding to PATTERN. (It does not compile it.) |
| 11 | """ |
| 12 | |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 13 | import re |
| 14 | |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 15 | _cache = {} |
Guido van Rossum | 762c39e | 1991-01-01 18:11:14 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | 762c39e | 1991-01-01 18:11:14 +0000 | [diff] [blame] | 17 | def fnmatch(name, pat): |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 18 | """Test whether FILENAME matches PATTERN. |
| 19 | |
| 20 | Patterns are Unix shell style: |
| 21 | |
| 22 | * matches everything |
| 23 | ? matches any single character |
| 24 | [seq] matches any character in seq |
| 25 | [!seq] matches any char not in seq |
| 26 | |
| 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). |
| 31 | """ |
| 32 | |
| 33 | import os |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 34 | name = os.path.normcase(name) |
| 35 | pat = os.path.normcase(pat) |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 36 | return fnmatchcase(name, pat) |
| 37 | |
| 38 | def fnmatchcase(name, pat): |
| 39 | """Test wheter FILENAME matches PATTERN, including case. |
| 40 | |
| 41 | This is a version of fnmatch() which doesn't case-normalize |
| 42 | its arguments. |
| 43 | """ |
| 44 | |
| 45 | if not _cache.has_key(pat): |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 46 | res = translate(pat) |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 47 | _cache[pat] = re.compile(res) |
| 48 | return _cache[pat].match(name) is not None |
Guido van Rossum | 762c39e | 1991-01-01 18:11:14 +0000 | [diff] [blame] | 49 | |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 50 | def translate(pat): |
Guido van Rossum | 7e4b2de | 1995-01-27 02:41:45 +0000 | [diff] [blame] | 51 | """Translate a shell PATTERN to a regular expression. |
| 52 | |
| 53 | There is no way to quote meta-characters. |
| 54 | """ |
| 55 | |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 56 | i, n = 0, len(pat) |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 57 | res = '' |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 58 | while i < n: |
Guido van Rossum | 762c39e | 1991-01-01 18:11:14 +0000 | [diff] [blame] | 59 | c = pat[i] |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 60 | i = i+1 |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 61 | if c == '*': |
| 62 | res = res + '.*' |
| 63 | elif c == '?': |
| 64 | res = res + '.' |
| 65 | elif c == '[': |
| 66 | j = i |
| 67 | if j < n and pat[j] == '!': |
| 68 | j = j+1 |
| 69 | if j < n and pat[j] == ']': |
| 70 | j = j+1 |
| 71 | while j < n and pat[j] != ']': |
| 72 | j = j+1 |
| 73 | if j >= n: |
| 74 | res = res + '\\[' |
| 75 | else: |
| 76 | stuff = pat[i:j] |
| 77 | i = j+1 |
| 78 | if stuff[0] == '!': |
| 79 | stuff = '[^' + stuff[1:] + ']' |
| 80 | elif stuff == '^'*len(stuff): |
| 81 | stuff = '\\^' |
| 82 | else: |
| 83 | while stuff[0] == '^': |
| 84 | stuff = stuff[1:] + stuff[0] |
| 85 | stuff = '[' + stuff + ']' |
| 86 | res = res + stuff |
Guido van Rossum | 05e5219 | 1992-01-12 23:29:29 +0000 | [diff] [blame] | 87 | else: |
Guido van Rossum | 9694fca | 1997-10-22 21:00:49 +0000 | [diff] [blame] | 88 | res = res + re.escape(c) |
| 89 | return res + "$" |