Regular expressions

Gary Herron gherron at islandtraining.com
Sun Oct 26 00:06:05 EDT 2003


On Saturday 25 October 2003 03:41 pm, Kill Bill wrote:
> I'm trying to find all combinations of the a string.
> I found that [blah]* gives it to me but it uses the same letter multiple
> times which I dont' want.

Several people have answered (correctly) one of your questions, that
being how to get the contents of a variable into a string.  

However, I think your other question remains unanswered, perhaps it is
not very well worded.  By saying you don't want it to not "uses the
same letter multiple times", I guess your asking about permutations of
a given string.  For instance the permutations of "abc" are

  abc
  acb
  bca
  bac
  cab
  cba

and not things like

  aac

Is this correct?

If so, you are a bit out of luck.  I don't think regular expressions
can do this in any straightforward way.  (However as you say regular
expressions are complex, so I won't claim that this is not possible.)

Perhaps you would be satisfied with something like this:

 "abc|acb|bca|bac|cab|cba"

and if you were clever enough to build a list of all permutations of a
given string

  listOfPermutations = Permutations("abc")  # e.g., ['abc', 'acb', ...]

then the regular expression could be gotten by

  '|'.join(listOfPermutations) # e.g., "abc|acb|..."


Hope that helps,
Gary Herron








More information about the Python-list mailing list