I have a situation where my regular expressions compile very quickly in Windows Server 2008. I wrote a small console application to highlight this problem. The application generates its own input and creates a regex from the words in the XML file. I created a version of this application and ran it both on my personal laptop (running XP) and on a Windows 2008 server. The regular expression was 0.21 seconds for compilation on my laptop, but 23 seconds for compilation on the server.
Any ideas what could be causing this? The problem is only when using Regex for the first time (when it is compiled first - after that, it's fine)
I also discovered another problem: when using \s+
in regular expression on the same Windows 2008 server, the memory balls (uses 4 GB +) and the Regex compilation never ends.
Is there a known issue with Regex and 64-bit .net? Is there a fix / fix for this? I canβt find any information on the net, but I found several articles about the same problems in Framework 2.0 - is this already fixed?
Additional information: The server runs a 64-bit version of the .net framework (3.5 SP1), and on my laptop I have Visual Studio 2008 and installed version 3.5. The regular expression has the following pattern: ^word$|^word$|^word$
and is created with the following flags: RegexOptions.IgnoreCase | RegexOptions.Compiled
RegexOptions.IgnoreCase | RegexOptions.Compiled
Here is the code snippet:
StringBuilder regexString = new StringBuilder(); if (!String.IsNullOrEmpty(fileLocation)) { XmlTextReader textReader = new XmlTextReader(fileLocation); textReader.Read(); while (textReader.Read()) { textReader.MoveToElement(); if (textReader.Name == "word") { regexString.Append("^" + textReader.GetAttribute(0) + "$|"); } } ProfanityFilter = new Regex(regexString.ToString(0, regexString.Length - 1), RegexOptions.IgnoreCase | RegexOptions.Compiled); } DateTime time = DateTime.Now; Console.WriteLine("\nIsProfane:\n" + ProfanityFilter.IsMatch("test")); Console.WriteLine("\nTime: " + (DateTime.Now - time).TotalSeconds); Console.ReadKey();
This results in 0.21 seconds on my laptop and 23 seconds on the 2008 server. The XML file consists of 168 words in the following format:
<word text="test" />
source share