Regular Expression AND mach

Jeff Epler jepler at unpythonic.net
Fri Mar 19 12:14:49 EST 2004


Regular expressions are not a good tool for this purpose.

Here's one bad solution:

def permutations:
    raise NotImplementedException, "exercise for the reader"

def rxseq(seq):  # Return a RE that has seq[0] followed by seq[1] etc
    return ".*".join(seq)

def rxand(seq):  # Return an RE that matches each permutation of seq
    return "|".join([rxseq(p) for p in permutations(seq)])

This fails when one part can overlap another, for instance if
word1="aba" and word2="b", "ab" or "ba".

You could also use a bunch of lookahead assertions, something like
    (?=.*word1)(?=.*word2)
but you'll also come to regret this choice.

Jeff
PS If you liked these regular expressions and would like to buy more,
visit e-bay where I'm selling a RE that matches multiples of 3 base 10.




More information about the Python-list mailing list