One more bit, add this on to the code in the previous post: print "collapse repeated characters" repeats.setParseAction(lambda s,l,toks: toks[0][0]) print test,"->",repeats.transformString(test) Gives: collapse repeated characters foo ooobaaazZZ -> fo obazZ