472,783 Members | 981 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,783 software developers and data experts.

Regex woes

I have these two methods that are chewing up a ton of CPU time in my
application. Does anyone have any suggestions on how to optimize them or
rewrite them without Regex? The most time-consuming operation by a long-shot
is the regex.Replace. Basically the only purpose of it is to remove spaces
between opening/closing tags and the element name. Surely there is a better

private string FixupJavascript(string htmlCode)
string result = FixupHTML(htmlCode);
result = result.Replace("\\", "\\\\");
result = result.Replace("\"", "\\\"");
Regex regex = new Regex("<\\s*script", RegexOptions.IgnoreCase);
result = regex.Replace(result, "<scr\" + \"ipt");
regex = new Regex("<\\s*\\/script>", RegexOptions.IgnoreCase);
result = regex.Replace(result, "</scr\" + \"ipt>");
result = result.Replace(Environment.NewLine,
string.Empty).Replace("\t", string.Empty).Replace("\n",

return result.Trim();

private string FixupHTML(string htmlCode)
string result = htmlCode;
Regex regex = new Regex("<\\s*\\/*html[^>]*>",
result = regex.Replace(result, string.Empty);
regex = new Regex("<\\s*head.*>.*<\\s*\\/head[^>]*>",
result = regex.Replace(result, string.Empty);
regex = new Regex("<\\s*\\/*body[^>]*>",
result = regex.Replace(result, string.Empty);
regex = new Regex("[^:]\\/\\/.*", RegexOptions.IgnoreCase);
result = regex.Replace(result, string.Empty);
return result;
Mar 18 '08 #1
0 1675

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
by: Tim Conner | last post by:
Is there a way to write a faster function ? public static bool IsNumber( char Value ) { if (Regex.IsMatch( Value.ToString(), @"^+$" )) { return true; } else return false; }
by: jeevankodali | last post by:
Hi I have an .Net application which processes thousands of Xml nodes each day and for each node I am using around 30-40 Regex matches to see if they satisfy some conditions are not. These Regex...
by: clintonG | last post by:
I'm using an .aspx tool I found at but as nice as the interface is I think I need to consider using others. Some can generate C# I understand. Your preferences please... <%= Clinton Gallagher ...
by: Extremest | last post by:
I have a huge regex setup going on. If I don't do each one by itself instead of all in one it won't work for. Also would like to know if there is a faster way tried to use string.replace with all...
by: Extremest | last post by:
I am using this regex. static Regex paranthesis = new Regex("(\\d*/\\d*)", RegexOptions.IgnoreCase); it should find everything between parenthesis that have some numbers onyl then a forward...
by: Trev | last post by:
Hi, If I had a string denoted by MyArray, how would I extract the number in brackets for use in the programme later on? I've tried "\" but all I get is a syntax error. What would be the usage for...
by: aspineux | last post by:
My goal is to write a parser for these imaginary string from the SMTP protocol, regarding RFC 821 and 1869. I'm a little flexible with the BNF from these RFC :-) Any comment ? tests= def...
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.