Regular expressions

Geoff Gerrietts geoff at gerrietts.net
Sun Oct 26 01:08:09 EDT 2003


Quoting Kill Bill (bill at kill.com):
> That's not what I want.  If I have yipee as "kdasfjh", then I want
> to compare to all combinations, so that there will be a match when
> it compares it with say when line is "fas" because line has all
> those letters in yipee. But "fass" will not match because yipee does
> not have two "s"'s.

The easiest way to do this with regular expressions is to do it with
several regular expressions rather than a single regular expression.
This still isn't easy.

To employ this solution, you would need to generate a regular
expression for each letter in your target. For each letter, the
pattern would look something like:

  [<all other letters>]*<this letter>[<all other letters>]*

Then you would match against the full suite of patterns. This gets
MORE complicated when your target has two of the same letter. Ouch.

My advice would be to not use regular expressions. The pattern you're
looking for can be pretty easily expressed in the following bit of
code:

  def is_a_permutation(check, yipee=pattern):
    list_of_letters = list(yipee)
    for letter in check:
      if letter in list_of_letters:
        list_of_letters.remove(letter)
      else:
        return False
    if list_of_letters == []:
      return True
    else:
      return False

It has the added advantage of being pretty clear.

Luck,
--G.

-- 
Geoff Gerrietts <geoff at gerrietts dot net>     http://www.gerrietts.net/
 "Politics, as a practice, whatever its professions, has always been the 
          systematic organization of hatreds." --Henry Adams





More information about the Python-list mailing list