Compare two lines with Regex

I use two lines for the corresponding program, for example:

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'm going to write a Regex matching function that compares each detail line between each "+" separately and calculates the percentage of matching, which is the number of matches found on each line. For example, in this example, we have the following matches:

 6 1 1 1 3000 12 21 1 1 1 -- 1 -- 1 1 

In this example, the percentage of compliance is 13 * 100/15 = 87%.

I am currently using the function below, but I think it is not optimized and using Regex might 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 ); } 
+6
source share
1 answer

The best solution would be the Levenshtein Word Distance algorithm. Some C # samples:

From matching characters, you can also calculate percentages.

+2
source

Source: https://habr.com/ru/post/946837/


All Articles