![](/img/trans.png)
[英]Using fnmatch.fnmatch(path, “*”) - does it match everything?
[英]fnmatch and recursive path match with `**`
是否有任何内置或直接的方法来递归匹配双星号的路径,例如像zsh那样?
例如,与
path = 'foo/bar/ham/spam/eggs.py'
我可以使用fnmatch来测试它
fnmatch(path, 'foo/bar/ham/*/*.py'
虽然,我希望能够做到:
fnmatch(path, 'foo/**/*.py')
我知道 fnmatch将其模式映射到 regex ,所以在单词的情况下,我可以使用额外的**
模式滚动我自己的 fnmatch,但也许有更简单的方法
如果您仔细查看 fnmatch 源代码,它会在内部将模式转换为正则表达式,将*
映射到.*
(而不是[^/]*
或类似的),因此不关心目录分隔符/
- 与 UNIX shell 不同:
while i < n:
c = pat[i]
i = i+1
if c == '*':
res = res + '.*'
elif c == '?':
res = res + '.'
elif c == '[':
...
因此
>>> fnmatch.fnmatch('a/b/d/c', 'a/*/c')
True
>>> fnmatch.fnmatch('a/b/d/c', 'a/*************c')
True
对于适用于路径的 fnmatch 变体,您可以使用名为wcmatch的库,该库实现了globmatch
函数,该函数匹配具有与glob
抓取文件系统相同的逻辑的路径。 您可以使用标志控制启用的功能,在这种情况下,我们启用GLOBSTAR
(使用**
进行递归目录搜索)。
>>> from wcmatch import glob
>>> glob.globmatch('some/file/path/filename.txt', 'some/**/*.txt', flags=glob.GLOBSTAR)
True
此代码段增加了对 ** 的兼容性
import re
from functools import lru_cache
from fnmatch import translate as fnmatch_translate
@lru_cache(maxsize=256, typed=True)
def _compile_fnmatch(pat):
# fixes fnmatch for recursive ** (for compatibilty with Path.glob)
pat = fnmatch_translate(pat)
pat = pat.replace('(?s:.*.*/', '(?s:(^|.*/)')
pat = pat.replace('/.*.*/', '.*/')
return re.compile(pat).match
def fnmatch(name, pat):
return _compile_fnmatch(str(pat))(str(name)) is not None
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.