Wednesday, 14 August 2013

Python reference to regex in parentheses

Python reference to regex in parentheses

I have a text file that needs to have the letter 't' removed if it is not
immediately preceded by a number.
I am trying to do this using re.sub and I have this:
f=open('File.txt').read()
g=f
g=re.sub('([^0-9])t','',g)
This identifies the letters to be removed correctly but also removes the
preceding character. How can I refer to the parenthesized regex in the
replacement String? Thanks!

No comments:

Post a Comment