def glob_to_regex(pattern):
 out = [u'^']
 components = pattern.strip(u'/').replace(u'.', u'[.]').split(u'/')
 doublestar = False
 for component in components:
  if (len(out) == 1):
   if pattern.startswith(u'/'):
    out.append(u'/')
  elif (not doublestar):
   out.append(u'/')
  if (u'**' in component):
   if (component != u'**'):
    raise ValueError(u'Invalid usage of "**", use "*" instead.')
   if (not doublestar):
    out.append(u'(([^/]+/)*)')
    doublestar = True
  else:
   out.append(component.replace(u'*', u'[^/]*'))
   doublestar = False
 if doublestar:
  out.append(u'[^/]*')
 out.append(u'$')
 return u''.join(out)

def glob_to_regex ( pattern ) : out = [ u'^' ] components = pattern . strip ( u'/' ) . replace ( u'.' , u'[.]' ) . split ( u'/' ) doublestar = False for component in components : if ( len ( out ) == 1 ) : if pattern . startswith ( u'/' ) : out . append ( u'/' ) elif ( not doublestar ) : out . append ( u'/' ) if ( u'**' in component ) : if ( component != u'**' ) : raise ValueError ( u'Invalid usage of "**", use "*" instead.' ) if ( not doublestar ) : out . append ( u'(([^/]+/)*)' ) doublestar = True else : out . append ( component . replace ( u'*' , u'[^/]*' ) ) doublestar = False if doublestar : out . append ( u'[^/]*' ) out . append ( u'$' ) return u'' . join ( out )
given a glob pattern .