Hey guys,
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!