Help with regular expression backreferences

David Lees deblNO at nospammytheworld.com
Tue Mar 27 23:48:08 EST 2001


I would like to use regular expression back references as keys into a
dictionary when doing substitution.  So far all I have is:

s='This xxMYJunkkxx the string that xxYourJunkxx the thing'
p=re.compile(r'xx(\w+)xx')
new_s=re.sub(p,'junk',s)
print new_s

which substitutes a fixed expression for the matches.  In this example
there are 2 matches, one of which is 'MYJunk' and the other 'YourJunk'
but I can not figure out how to get at the backreference values in the
re.sub

Thanks in advance.

David Lees



More information about the Python-list mailing list