Is it possible to check multiple regex matches at once? at one iteration?

Ok my question is about checking performanceLets say i have the following regexes regex1 = "=powered\ by\ 4images" regex2 = "post\ comment" regex3 = "kommentar\ posten" regex4 =...

Ok my question is about checking performance

Lets say i have the following regexes

    regex1 = "=powered\ by\ 4images"
    regex2 = "post\ comment"
    regex3 =  "kommentar\ posten"
    regex4 = "\[url=.*?.*?.*?.*?://"
    string mystring="";

Now what i want is this

    if( Regex.IsMatch(srinput, regex1 , RegexOptions.IgnoreCase) 
&& Regex.IsMatch(srinput, regex2 , RegexOptions.IgnoreCase) 
&& (Regex.IsMatch(srinput, regex3 , RegexOptions.IgnoreCase) 
|| Regex.IsMatch(srinput, regex4 , RegexOptions.IgnoreCase)))

So this will make for each option a full scan query of string text right ?

Can this be fasten up anyway ? I don't know in one iteration check all regexes etc ?

Is this the most performance way of achieving multiple regex IsMatch check ?

c# .net 4.5.2 wpf application

You can use positive lookahead assertions to simulate a Boolean AND (&&) and alternation to simulate a Boolean OR (||) in a regular expression.

Then, you don't need to escape a space character in a regex, but you should escape the dot if you want it to match a literal dot. I assume you meant something like this:

Regex regex1 = new Regex(@"
 ^                           # Either make sure that at the start of the string
 (?=.*=powered by 4images)   # it's possible to match this subregex
 (?=.*post comment)          # and this one
 (?=.*kommentar posten)      # and this one 
|                            # or
 \[url=.*?\..*?\..*?\..*?:// # match this regex anywhere in the string", 
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);

the static Regrex.IsMatch internally calls return new Regex(pattern, options, matchTimeout, true).IsMatch(input);if you perform a lot of checks, and maybe create an instance and reuse it will save some performance. Here's the code:

string mystring = "MonsterMMORPG";
        var sw = new Stopwatch();
        sw.Start();
        int count = 10000;
        var regex1 = @"=powered\ by\ 4images";
        for (int i = 0; i < count; i++)
        {


            if (Regex.IsMatch(mystring, regex1, RegexOptions.IgnoreCase))
            {

            }
        }
        sw.Stop();
        Console.WriteLine(string.Format("using Static Check:{0}", sw.Elapsed));

        sw = new Stopwatch();
        var r = new Regex(regex1,RegexOptions.IgnoreCase);
        sw.Start();
        for (int i = 0; i < count; i++)
        {
            if (r.IsMatch(mystring))
            {

            };
        }
        sw.Stop();
        Console.WriteLine(string.Format("using instance Check:{0}", sw.Elapsed));

The output is: using Static Check:00:00:00.0074411 using instance Check:00:00:00.0006221

the regrex check is pretty fast either way, but the instance one is slightly faster

Just add a | between each conditions of your regex, then match at once.

(=powered\ by\ 4images)|(post\ comment)|(kommentar\ posten)|(\[url=.*?.*?.*?.*?:)

https://regex101.com/r/yW0nQ5/2

Translating... 0%