Compare two lines ignoring specific characters

I wonder if there is an easy way to check if two lines match, excluding certain characters in the lines. See the example below.

I can easily write such a method by writing a regular expression to find the wild card characters and replace them with a common character. Then compare the two lines str1 and str2. I am not looking for such implementations, but would like to know if there are any .Net wireframe classes that can take care of this. It seems like a general need, but I could not find such a method.

For example:

string str1 = "ABC-EFG";    
string str2 = "ABC*EFG";

Two lines must be declared equal.

Thanks!

+3
source share
5 answers

, , " " . , , , .

+1

, , , , String.Compare:

String.Compare(str1, str2, CultureInfo.InvariantCulture, CompareOptions.IgnoreSymbols)
+8

, :

- , .

DLD "ABC-EFG" "ABC * EFG" - 1- " , , , ."

, 1 "ZBC-EFG" "ABC-EFG" - , , .

Python DLD implementation from http://paxe.googlecode.com/svn/trunk/paxe/Lib/Installer.py :

def dist(s1, s2):
    d = {}
    lenstr1 = len(s1)
    lenstr2 = len(s2)
    for i in xrange(-1,lenstr1+1):
        d[(i,-1)] = i+1
    for j in xrange(-1,lenstr2+1):
        d[(-1,j)] = j+1

    for i in xrange(0,lenstr1):
        for j in xrange(0,lenstr2):
            if s1[i] == s2[j]:
                cost = 0
            else:
                cost = 1
            d[(i,j)] = min(
                d[(i-1,j)] + 1, # deletion
                d[(i,j-1)] + 1, # insertion
                d[(i-1,j-1)] + cost, # substitution
                )
            if i>1 and j>1 and s1[i]==s2[j-1] and s1[i-1] == s2[j]:
                d[(i,j)] = min (d[(i,j)], d[i-2,j-2] + cost) # transposition

    return d[lenstr1-1,lenstr2-1]
+4
source

Of course, you can check for replacement with regex:

[a-zA-z]{3}.[a-zA-z]{3}

It seems like the common use of regular expressions, so why avoid it?

+1
source

No, in the structure itself there is nothing that could do this.

0
source

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


All Articles