Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8418687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:23:49+00:00 2026-06-10T02:23:49+00:00

I am trying to remove a block of text in the apache configuration file,

  • 0

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))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T02:23:50+00:00Added an answer on June 10, 2026 at 2:23 am

    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 VirtualHost tags. AFAIK, there’s not a way to get a file’s entire contents using fileinput, 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:

    import os
    import sys
    import re
    
    for fn in sys.argv[1:]:
        os.rename(fn, fn + '.orig')
        with open(fn + '.orig', 'rb') as fin, open(fn, 'wb') as fout:
            data = fin.read()
            data = re.sub(r'<VirtualHost.*?>.*?</VirtualHost>', '', data,
                          flags=re.DOTALL)
            fout.write(data)
    

    (This requires Python 2.7, because I’m using the built-in syntax for nested contexts in the with statement, but you can get the same functionality with earlier versions using contextlib.nested.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(in PHP ... ) I'm trying to take a block of text and remove
I'm trying to remove wiki formatting from some text so it can be parsed.
I'm trying to find a block of text and replace it with a modified
I am trying to create a chrome extension that will remove/block/hide a piece of
I'm trying to delete some things from a block of text using regex. I
I'm trying to remove (just replace it with ) the following String: <script type=text/javascript>
I'm trying to remove any duplicate or more occurrences of any < br >
I'm trying to remove part of the path in a string. I have the
I'm trying to remove scrollbar from my facebook app. I tried js solution, that
I'm trying to remove a specific graph object from the zedgraph, is it possible....?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.