Find and replace multiple RegEx search expressions

J

Jignesh Sutar

Hi,

I'm trying to delete contents of a .txt log file, matching on multiple
re.sub criteria but not sure how to achieve this.

Below is an illustration of what I am trying to achieve (of course in this
example I can combine the 3 re.sub into a single re expression but my
actual code will have a dozen plus expression I need to match on so easier
to keep them separate). Only the last re.sub will take effect in the
example below I need all 3 to take effect.


import re
o = open(r"c:\temp\outputfile.txt","w")
data = open(r"C:\Temp\infile.txt").read()
o.write( re.sub(".*<X> ","",data) )
o.write( re.sub(".*<Y> ","",data) )
o.write( re.sub(".*<Z> ","",data) )
o.close()


Thanks in advance.
Jignesh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top