I am trying to remove a block of text in the apache configuration file, specifically virtualhosts. I need to remove the virtualhost containers including the < VirtualHost> markers.
Stuff
<VirtualHost asdfalsdkf:*>
asldkfjasl;dkfjasldkfj
asdfljasldjf;laksdfj
a;lsdkfj;laksjdfas
asldkfjasldfkj
3495034ijfgdl9)_*)(%$
more stuff
</VirtualHost>
stuff
So far I have tried to regex it out. but it is not changing the file. I am actually trying to update the file and remove the code.
This is what I have so far that is not working.
for line in fileinput.input('/etc/apache2/apache2.conf.replace',inplace=True):
sys.stdout.write(re.sub(r'<VirtualHost.*?>.*?</VirtualHost>','',line))
There are two issues here. The first is (as javex pointed out) that you need to use
re.DOTALL.But that’s not enough. You’re still only feeding the regex one line at a time, so it will never see both the opening and closing
VirtualHosttags. AFAIK, there’s not a way to get a file’s entire contents usingfileinput, but assuming you don’t need to accept input from STDIN and there files will be small enough to read into memory all at once (which should be the case for Apache config files), this should do it:(This requires Python 2.7, because I’m using the built-in syntax for nested contexts in the
withstatement, but you can get the same functionality with earlier versions usingcontextlib.nested.)