Compare two strings by ignoring certain characters

前端 未结 5 773
梦毁少年i
梦毁少年i 2021-01-20 07:38

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

I can easily write such a method by

相关标签:
5条回答
  • 2021-01-20 07:52

    Sorry but I think either regex, or replacing the "wildcard" characters with a common character are going to be your best solution. Basically, the answers that you stated you didn't want to receive.

    0 讨论(0)
  • 2021-01-20 07:55

    I found myself having the same requirements, the solution I used was based on the String.Compare method:

    String.Compare(str1, str2, CultureInfo.InvariantCulture, CompareOptions.IgnoreSymbols)
    
    0 讨论(0)
  • 2021-01-20 08:03

    You can of course test the regex w/out substitution:

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

    Seems like a common use for regex, so why the avoidance?

    0 讨论(0)
  • 2021-01-20 08:13

    Not sure if this helps:

    The Damerau-Levenshtein distance is one of several algorithms dealing with fuzzy string searching.

    The DLD between "ABC-EFG" and "ABC*EFG" is 1—"the minimum number of operations needed to transform one string into the other, where an operation is defined as an insertion, deletion, or substitution of a single character, or a transposition of two characters."

    Of course this algorithm would also return 1 for the two strings "ZBC-EFG" and "ABC-EFG"—possibly not what you are looking for.

    An implementation of the DLD, in Python, 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]
    
    0 讨论(0)
  • 2021-01-20 08:14

    No, there is nothing in the framework itself that can do this.

    0 讨论(0)
提交回复
热议问题