def ordered_permutation_regex(sentence):
 sentence = _RE_REF.sub('\\1', sentence)
 sentence = _RE_REF_LANG.sub('\\1', sentence)
 sentence = _RE_SELF_REF.sub('', sentence)
 words = sentence.split()
 combinations = itertools.product((True, False), repeat=len(words))
 solution = []
 for combination in combinations:
  comb = []
  for (iword, word) in enumerate(words):
   if combination[iword]:
    comb.append(word)
   elif comb:
    break
  if comb:
   solution.append((_PREFIX + ('[0-9]*%s*%s(?=\\W|$)+' % (_NUM_SEP, re_escape(' '.join(comb)).rstrip('\\')))))
 regex = '|'.join(sorted(set(solution), key=(lambda o: len(o)), reverse=True))
 return regex

def ordered_permutation_regex ( sentence ) : sentence = _RE_REF . sub ( '\\1' , sentence ) sentence = _RE_REF_LANG . sub ( '\\1' , sentence ) sentence = _RE_SELF_REF . sub ( '' , sentence ) words = sentence . split ( ) combinations = itertools . product ( ( True , False ) , repeat = len ( words ) ) solution = [ ] for combination in combinations : comb = [ ] for ( iword , word ) in enumerate ( words ) : if combination [ iword ] : comb . append ( word ) elif comb : break if comb : solution . append ( ( _PREFIX + ( '[0-9]*%s*%s(?=\\W|$)+' % ( _NUM_SEP , re_escape ( ' ' . join ( comb ) ) . rstrip ( '\\' ) ) ) ) ) regex = '|' . join ( sorted ( set ( solution ) , key = ( lambda o : len ( o ) ) , reverse = True ) ) return regex
builds a regex that matches ordered permutations of a sentences words .