简体   繁体   中英

Very slow regex perfomance?

My application is very slow and sometimes it take hours to become normal. When i used profiler i found the code where it is taking large amount of time which is nothing but the place where regex match occurs. Can any body guide me how to improve the perfomance. The code snippet is shown below

Regex rx = new Regex(@"^[A-Za-z0-9]([_\.\-]?[A-Za-z0-9]+)*\@[A-Za-z0-9]([_\.\-]?[A-Za-z0-9]+)*\.[A-Za-z0-9]([_\.\-]?[A-Za-z0-9]+)*$|^$");
rx.IsMatch("john.gilbert.stu.seattle.washington.us"); 

Is there any way i can cache the patterns and reuse it?

You can somewhat speed up RegEx by compiling and caching them, but it is very unlikely to solve performance issue on the scale you have. Ie some slow RegEx that requires O(n^2) will not magically become O(n) due to caching/compiling or any other sort of automatic processing.

You need to review you regular expressions and validate if number of times each is executed. The fastest code is the code that does not need to run at all - so eliminate wasted matches if you have any first. You may need to switch to more appropriate way of parsing text (ie RegEx parsing of HTML is most likely wrong way - some good HTML parser like HtmlAgilityPack combined with targeted queries may be more appropriate).

If the strings aren't that complicated to parse I would just convert them to character arrays and parse them myself. It will dramatically improve performance. RegEx has very poor performance.

for (int i = 0; i < string.Length; i++)
{
     if (string[i] has some defining quality)
         if (string[i] meets second requirement)
         // break, change flag, ect.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM