How to insert string in each match using RegEx iterator

Paul McGuire ptmcg at
Wed Jun 10 06:17:01 EDT 2009

On Jun 9, 11:13 pm, "504cr... at" <504cr... at> wrote:
> By what method would a string be inserted at each instance of a RegEx
> match?

Some might say that using a parsing library for this problem is
overkill, but let me just put this out there as another data point for
you.  Pyparsing ( supports callbacks
that allow you to embellish the matched tokens, and create a new
string containing the modified text for each match of a pyparsing
expression.  Hmm, maybe the code example is easier to follow than the

from pyparsing import Word, nums, Regex

# an integer is a 'word' composed of numeric characters
integer = Word(nums)

# or use this if you prefer
integer = Regex(r'\d+')

# attach a parse action to prefix 'INSERT ' before the matched token
integer.setParseAction(lambda tokens: "INSERT " + tokens[0])

# use transformString to search through the input, applying the
# parse action to all matches of the given expression
test = '123 abc 456 def 789 ghi'
print integer.transformString(test)

# prints
# INSERT 123 abc INSERT 456 def INSERT 789 ghi

I offer this because often the simple examples that get posted are
just the barest tip of the iceberg of what the poster eventually plans
to tackle.

Good luck in your Pythonic adventure!
-- Paul

More information about the Python-list mailing list