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 403365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:12:36+00:00 2026-05-12T17:12:36+00:00

I have a Regex based whitespace filter on an ASP.NET MVC application, and it

  • 0

I have a Regex based whitespace filter on an ASP.NET MVC application, and it works perfectly, too perfectly. One of the things that gets filtered are the \r\n characters. This effectively makes everything in one line of source code, which I love because I don’t have to deal with quirky CSS because of the whitespace, but in certain instances I need to retain them. One example is when I want to literraly display text with line breaks in it, such as a note.

To do so, I would obviously wrap it in <pre></pre> tags, but because of the filter the linebreaks of text in between the tags also gets scrubbed, so it makes a note for example rather difficult to read.

Can anyone with Regex knowledge (mine is very poor…) help me in modifying the current Regex to ignore text between the <pre> tags?

Here’s the current code:

public class WhitespaceFilter : MemoryStream {
    private string Source = string.Empty;
    private Stream Filter = null;

    public WhitespaceFilter(HttpResponseBase HttpResponseBase) {
        Filter = HttpResponseBase.Filter;
    }

    public override void Write(byte[] buffer, int offset, int count) {
        Source = UTF8Encoding.UTF8.GetString(buffer);

        Source = new Regex("\\t", RegexOptions.Compiled | RegexOptions.Multiline).Replace(Source, string.Empty);
        Source = new Regex(">\\r\\n<", RegexOptions.Compiled | RegexOptions.Multiline).Replace(Source, "><");
        Source = new Regex("\\r\\n", RegexOptions.Compiled | RegexOptions.Multiline).Replace(Source, string.Empty);

        while (new Regex("  ", RegexOptions.Compiled | RegexOptions.Multiline).IsMatch(Source)) {
            Source = new Regex("  ", RegexOptions.Compiled | RegexOptions.Multiline).Replace(Source, string.Empty);
        };

        Source = new Regex(">\\s<", RegexOptions.Compiled | RegexOptions.Multiline).Replace(Source, "><");
        Source = new Regex("<!--.*?-->", RegexOptions.Compiled | RegexOptions.Singleline).Replace(Source, string.Empty);

        Filter.Write(UTF8Encoding.UTF8.GetBytes(Source), offset, UTF8Encoding.UTF8.GetByteCount(Source));
    }
}

Thanks in advance!

  • 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-05-12T17:12:36+00:00Added an answer on May 12, 2026 at 5:12 pm

    There are tools like htmlcompressor already out there to strip whitespace. And like exhuma said, if this is for web optimization then gzip compression would help more than anything if you configured it on the web server.

    As for your original question, there a lot of different ways to do this. You could also attack the problem with something like XPATH (if the HTML is valid XHTML) and then combine that with regex. But I figured I’d try my hand at writing a single regex to do it:

    (<pre>[^<>]*(((?<Open><)[^<>]*)+((?<Close-Open>>)[^<>]*)+)*(?(Open)(?!))</pre>)|[\n\r]
    

    It seems to work for me. Fortunately .NET has an extremely powerful regex engine including a very cool balanced matching feature. I can’t explain it any better than Ryan Byington can. But the idea is to match the beginning and ending pre tags first and make sure everything inside is untouched. Then everything around those pre tags gets the rest of the regex applied, “[\n\r]”.

    To make this work you’d simply do this:

    Source = new Regex("(<pre>[^<>]*(((?<Open><)[^<>]*)+((?<Close-Open>>)[^<>]*)+)*(?(Open)(?!))</pre>)|[\n\r]", 
      RegexOptions.Compiled | RegexOptions.Singleline).Replace(Source, "$1");
    

    Note the $1 at the end. This is the part that grabs the results from inside the pre tags and returns them untouched.

    Then after that write another line to replace \s\s+ with a single space. I think that should work pretty well.

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

Sidebar

Related Questions

I have this regex that detects hashtags. It shouldn't match things with letters before
I need to have a RegEx that will match a URI like this based
I have the following VB.Net 2.0 in an ASP.Net app: output = Regex.Replace(output, <p>(?:(?:\<\!\-\-.*?\-\-\>)|&(?:nbsp|\#0*160|x0*A0);|<br\s*/?>|[\s\u00A0]+)*</p>,
I have a regex I'm running to filter rows in a table. The filtering
I have this regex that checks for a 5 digit number ^\d{5}$ How do
I have this regex that uses forward and backward look-aheads: import re re.compile(<!inc\((?=.*?\)!>)|(?<=<!inc\(.*?)\)!>) I'm
I have some Regex, it looks like this: string regexForDrop = @^((%27)|'|(\-\-))\s*(d|%64|%44)(r|%72|%52)(o|%6F|%4F)(p|%70|%50)$; It works
I have a regex expression that I'm doing a split against another string and
I have this simple regex replace based routine, is there anyway to improve its
I have a web application, written in PHP that incorporates Javascript and JQuery, that

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.