all Python 3fnmatchfunction
fnmatch.translate()
filename patterns
Translates a shell-style pattern into a regular expression string that you can compile with re.compile().
Syntax
regex = fnmatch.translate(pat)
The result matches the whole string and is meant for re.match(); re.search() is not equivalent: the regex is anchored only at the end, not at the start, so search also matches a suffix of the string. The exact regex text changes between versions (3.14 replaced \Z with \z), so never compare it to a string in tests; check behaviour instead. Unlike glob.translate(), it knows nothing about '/' or hidden files.
Examples
import fnmatch, re
rx = re.compile(fnmatch.translate('*.py'))
print(bool(rx.match('main.py')), bool(rx.match('main.pyc'))) # → True False
print(fnmatch.translate('*.py')) # → ? (?s:.*\.py)\z на 3.14+, \Z раньше
See also
Open in the full glossarydocs.python.orgReport an inaccuracy