468,278 Members | 1,566 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,278 developers. It's quick & easy.

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
way.

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",
string.Empty).Trim();

return result.Trim();
}

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tim Conner | last post: by
20 posts views Thread by jeevankodali | last post: by
17 posts views Thread by clintonG | last post: by
6 posts views Thread by Extremest | last post: by
7 posts views Thread by Extremest | last post: by
8 posts views Thread by Trev | last post: by
3 posts views Thread by aspineux | last post: by
15 posts views Thread by morleyc | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.