return joinmapreplace separatepattern def separatepattern return re finditerr pset pattern def replacematch return match group set or re escapematch group0 replace r replace r replace r given a glob pattern produce a regex that matches it translate txt txt translate a txt a txt translate separate out character sets to avoid translating their contents m group 0 for m in separate txt txt m group 0 for m in separate a txt a txt perform the replacements for a match from func separate
import re


def translate(pattern):
    r
    return ''.join(map(replace, separate(pattern)))


def separate(pattern):
    
    return re.finditer(r'([^\[]+)|(?P<set>[\[].*?[\]])|([\[][^\]]*$)', pattern)


def replace(match):
    

    return match.group('set') or (
        re.escape(match.group(0))
        .replace('\\*\\*', r'.*')
        .replace('\\*', r'[^/]*')
        .replace('\\?', r'.')
    )