Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex IsMatch stops matching

I’m writing a checker with Regex, and I’m testing results with one of the popular tools:
https://regex101.com/r/DeMWEy/1

I have a list of strings that should match:

Process(Bystronic.Ads.GraphQL)\% Processor Time
Process(Bystronic.ParameterTuning.WebApi.Service)\\% Processor Time
Process(Bystronic.Ads.GraphQL)\\IO Write Bytes/sec
Process(DcCutting)\\IO Read Bytes/sec
Process(DcCutting)\\% Processor Time

And a regex that should match all of them.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"Process(.+)\\.+"

Here is a MVC:

using System.Text.RegularExpressions;

public class MainClass
{

    private static bool IsProcessMatch(string column)
    {
        return Regex.IsMatch(column, "Process(.+)\\.+");
    }

    public static void Main()
    {
        var result1 = IsProcessMatch("Process(Bystronic.Ads.GraphQL)\\% Processor Time");
        var result2 = IsProcessMatch("Process(Bystronic.ParameterTuning.WebApi.Service)\\% Processor Time");
        var result3 = IsProcessMatch("Process(Bystronic.Ads.GraphQL)\\IO Write Bytes/sec");
        var result4 = IsProcessMatch("Process(DcCutting)\\IO Read Bytes/sec");
        var result5 = IsProcessMatch("Process(DcCutting)\\% Processor Time");

        Console.WriteLine(result1);
        Console.WriteLine(result2);
        Console.WriteLine(result3);
        Console.WriteLine(result4);
        Console.WriteLine(result5);
    }
}

I’m getting a match in the first and second one, but not in the rest. Can somebody explain me the difference between them?

>Solution :

C# treats the pattern a little different than the regex engine used page that you posted. On the left side there is a menue where you can choose the flavor. If you choose C# you will see:

enter image description here

that one little detail is missing: @ in your code. The page will add it for you

private bool IsProcessColumn(string column)
{
    return Regex.IsMatch(column, @"Process(.+)\\.+");
}

Here is a fiddle for testing

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading