Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

c# - Optimize performance with multiple calls to Regex.IsMatch on large text

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...