Match First Sequence in Regular Expression?

Tim Chase python.list at
Thu Jan 26 11:28:11 EST 2006

>>r = re.compile("[^a]*a{3}b+(a+b*)*")
>>matches = [s for s in listOfStringsToTest if r.match(s)]
> Wow, I like it, but it allows some strings it shouldn't.  For example:
> "xyz123aabbaaab"
> (It skips over the two-letter sequence of 'a' and matches 'bbaaab'.)

Anchoring it to the beginning/end might solve that:

	r = re.compile("^[^a]*a{3}b+(a+b*)*$")

this ensures that no "a"s come before the first 3x"a" and nothing 
but "b" and "a" follows it.

(who's translating from vim regexps which are just diff. enough 
to throw a wrench in works...)

More information about the Python-list mailing list