Efficiently querying one string against multiple regexes

前端 未结 18 886
感情败类
感情败类 2020-12-12 17:16

Lets say that I have 10,000 regexes and one string and I want to find out if the string matches any of them and get all the matches. The trivial way to do it would be to jus

18条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-12 17:44

    The fastest way to do it seems to be something like this (code is C#):

    public static List FindAllMatches(string s, List regexes)
    {
        List matches = new List();
        foreach (Regex r in regexes)
        {
            if (r.IsMatch(string))
            {
                matches.Add(r);
            }
        }
        return matches;
    }
    

    Oh, you meant the fastest code? i don't know then....

提交回复
热议问题