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

  • Home
  • SEARCH
  • 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 430809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:54:43+00:00 2026-05-12T19:54:43+00:00

I have a set of html files that I want to modify by replacing

  • 0

I have a set of html files that I want to modify by replacing the header and footer. The contents of each file is different and I would like to use a regular expression (or similar if RE can’t handle multiline queries).

As an example, one modification I want to make is to replace everything between <html> and </head> with a standard header.

Can this be done with a regular expression? What method would you use to perform a bulk search and replace like this in C#?

Can you provide an example of a regular expression that matches multiple lines?

  • 1 1 Answer
  • 3 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-05-12T19:54:44+00:00Added an answer on May 12, 2026 at 7:54 pm

    Well the simple answer is, yes.

    Regex could indeed help you, but you need a tool that copes with multiple files. I can’t recommend any at the moment, try Googling “multiple file search and replace”. Regex can cope with multi-line or single-line matching.

    I use Notepad++ which can sort of do what you want to do a search/replace in multiple files (open or within a directory tree), not it’s primary aim, but it works.

    The hard part is defining your “match” making sure that where you want to pick out details you need to preserve that you have an appropriate capture group that you can use in your “replace” expression.

    So, again, yes it can help, but your question is very high level.

    For the C# part, it’s simple once you have your regex defined.

    static void Main()
    { 
         // Remove everything (by commenting out) everything between HTML
         // and the end of the HEAD tag.
         string matchRegex = "<html[^>]*>(.*?)</head>";
         string replaceExpression = "<html> <!-- \0 </head> -->";
    
         string pattern = "*.html";
    
         using ( DirectoryInfo di = new DirectoryInfo(.) )
         {
              foreach (FileInfo fi in di.GetFiles(pattern))
              {
                   using ( StreamReader sr = fi.OpenText() )
                   {
                        // Going from memory here, may need to use a TextReader...
                        string content = fi.ReadToEnd();
    
                        // Treat as single-line so that the match can span
                        // several lines.
                        string newContent = Regex.Replace(content, 
                                                          matchRegex, 
                                                          replaceExpression,
                                                          RegexOptions.Singleline);
    
                        // Write-out/overwirte your new file here....
                   }
              }
         }
    }
    

    You may find this page useful, in it, someone is trying to write a regular expression to match comments, then handle multiple line comments, etc. It shows the regex thought process. Finding Comments in source code. The replace part is easy, put a capture group in and reference the group/name in the replacement string!

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

Sidebar

Related Questions

I have a text file of HTML that includes the following marker set for
I have a PHP page that returns a piece of HTML to set the
I want to achieve this using html and css: I have tried to set
I have a HTML form; I want to be able to set it so
I have a few HTML files that I'd like to include via tags in
I have a set of complex Excel files (with figures in it) that I
I have a bunch of large HTML files and I want to run a
I have used the a jquery script that is used to upload files: http://aquantum-demo.appspot.com/file-upload
let's say I have this set of HTML-markup and CSS #CSS .inputhelp_text { background:
Hi all I have problem with my div's in html. I have set them

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.