Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare two strings using Regex

Tags:

c#

regex

I am using two strings for a matching program like this:

string s1= 5-4-6-+1-+1+1+3000+12+21-+1-+1-+1-+2-3-4-5-+1-+10+1-+1-+;
string s2= 6-+1-+1+1+3000+12+21-+1-+1-+1-+1-+1-+1+1-+1-+;

And I am going to write a Regex matching function which compares each part string between each "+" separately and calculates the match percent, which is the number of matches occurring in each string. For example in this example we have these matches:

6

1

1

1

3000

12

21

1

1

1

--

1

--

1

1

In this example the match percent is 13*100/15=87%.

Currently I am using the function below, but I think it is not optimized and using Regex may be faster.

public double MatchPercent(string s1, string s2) {
    int percent=0;
    User = s1.Split('+').ToArray();
    Policy = s2.Split('+').ToArray();

    for (int i = 0; i < s1.Length - 2; i++) {
        int[] U = User[i].Split('-').Where(a => a != "").Select(n => 
                      Convert.ToInt32(n)).Distinct().ToArray();
        int[] P = Policy[i].Split('-').Where(a => a != "").Select(n => 
                      Convert.ToInt32(n)).Distinct().ToArray();
        var Co = U.Intersect(P);
        if (Co.Count() > 0) {
            percent += 1;
        }
    }
    return Math.Round((percent) * 100 / s1.Length );
}
like image 694
Kamran Avatar asked Jun 08 '13 08:06

Kamran


1 Answers

A better solution would be Levenshtein Word Distance algorithm. Some C# samples:

  • http://www.dotnetperls.com/levenshtein
  • http://blogs.msdn.com/b/toub/archive/2006/05/05/590814.aspx

From the matching characters you can also calculate the percentages.

like image 115
Andras Sebo Avatar answered Oct 05 '22 17:10

Andras Sebo