Murat
Murat

Reputation: 803

Compare 2 string with Regex (C#)

I have a correct string and then a string come anywhere. I must compare but this strings maybe not equal. Example Correct string is

SAAT:23:34

Coming string

SAAT:12:23

When I compare this strings, Answer must be true.

Patern like this

SAAT:..:..
Regex.IsMatch(); 

give me t string but ı dont want to this.

How can I compare two string..

Upvotes: 0

Views: 7417

Answers (1)

Andrew T Finnell
Andrew T Finnell

Reputation: 13628

Based on the information you've provided and my lack of caffeine here is a solution:

    static bool IsEqual(String left, String right)
    {
        left = Regex.Replace(left, ":[0-9]*:[0-9]*", "");
        right = Regex.Replace(right, ":[0-9]*:[0-9]*", "");
        return left.Equals(right);
    }

    static void Main(string[] args)
    {
        Console.WriteLine(IsEqual("SAAT:232:34", "SAAT:12:23")); // True
        Console.WriteLine(IsEqual("PAAT:23:34", "SAAT:12:23")); // False
        Console.WriteLine(IsEqual("SAAT:23:34:HAT", "SAAT:12:23:HAT")); // True
    }

Upvotes: 2

Related Questions