Match First Sequence in Regular Expression?

Tim Chase python.list at tim.thechases.com
Thu Jan 26 10:42:16 EST 2006


> Sorry for the confusion.  The correct pattern should reject
> all strings except those in which the first sequence of the
> letter 'a' that is followed by the letter 'b' has a length of
> exactly three.

Ah...a little more clear.

	r = re.compile("[^a]*a{3}b+(a+b*)*")
	matches = [s for s in listOfStringsToTest if r.match(s)]

or (as you've only got 3 of 'em)

	r = re.compile("[^a]*aaab+(a+b*)*")
	matches = [s for s in listOfStringsToTest if r.match(s)]

should do the trick.  To exposit:

	[^a]*	a bunch of stuff that's not "a"

	a{3} or aaa	three letter "a"s

	b+	one or more "b"s

	(a+b*)	any number of "a"s followed optionally by "b"s

Hope this helps,

-tkc











More information about the Python-list mailing list