简体   繁体   English

使用正则表达式比较两个字符串

[英]Compare two strings using 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. 我将编写一个Regex匹配函数,该函数分别比较每个“ +”之间的每个部分字符串,并计算匹配百分比,这是每个字符串中发生的匹配数目。 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%. 在此示例中,匹配百分比为13 * 100/15 = 87%。

Currently I am using the function below, but I think it is not optimized and using Regex may be faster. 目前,我正在使用下面的函数,但我认为它尚未优化,使用Regex可能会更快。

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 );
}

A better solution would be Levenshtein Word Distance algorithm. 更好的解决方案是Levenshtein单词距离算法。 Some C# samples: 一些C#示例:

From the matching characters you can also calculate the percentages. 从匹配的字符,您还可以计算百分比。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM