Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a long text (50-60 KB) and I need to run several regular expressions against it (about 100 rules in total). However, this is so slow that it essentially doesn't work.

All I have done is created a loop around the rules where each rule does a Regex.IsMatch().

Is there a way to optimize this?

UPDATE

Sample code of what each rule is doing:

public class SomeRegexInterceptor : ValidatorBase
    {
        private readonly Regex _rgx = new Regex("some regex", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Singleline); 

        public override void Intercept(string html, ValidationResultCollection collection)
        {
            if (!_rgx.IsMatch(html)) return;

            /* do something irrelevant here */
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
272 views
Welcome To Ask or Share your Answers For Others

1 Answer

The most important thing about the usage of Regex replacements is how and where you declare your Regex. Never initialize a Regex object inside a loop.

Create a static class and add public static readonly Regex fields with RegexOptions.Compiled flag set.

Then, use them wherever you need using something like MyRegexClass.LeadingWhitespace.Replace(str, string.Empty).

Note that if you need to use Regex.Replace, you do not need to check if there is a match with Regex.IsMatch before.

Read and follow the recommendations outlined at Best Practices for Regular Expressions in the .NET Framework, namely:

Also, consider processing the file line by line, and avoid regular expressions wherever you can do without them.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...