code_junkie

Compare two strings by ignoring certain characters

Nov 14th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. string str1 = "ABC-EFG";
  2. string str2 = "ABC*EFG";
  3.  
  4. [a-zA-z]{3}.[a-zA-z]{3}
  5.  
  6. def dist(s1, s2):
  7. d = {}
  8. lenstr1 = len(s1)
  9. lenstr2 = len(s2)
  10. for i in xrange(-1,lenstr1+1):
  11. d[(i,-1)] = i+1
  12. for j in xrange(-1,lenstr2+1):
  13. d[(-1,j)] = j+1
  14.  
  15. for i in xrange(0,lenstr1):
  16. for j in xrange(0,lenstr2):
  17. if s1[i] == s2[j]:
  18. cost = 0
  19. else:
  20. cost = 1
  21. d[(i,j)] = min(
  22. d[(i-1,j)] + 1, # deletion
  23. d[(i,j-1)] + 1, # insertion
  24. d[(i-1,j-1)] + cost, # substitution
  25. )
  26. if i>1 and j>1 and s1[i]==s2[j-1] and s1[i-1] == s2[j]:
  27. d[(i,j)] = min (d[(i,j)], d[i-2,j-2] + cost) # transposition
  28.  
  29. return d[lenstr1-1,lenstr2-1]
  30.  
  31. String.Compare(str1, str2, CultureInfo.InvariantCulture, CompareOptions.IgnoreSymbols)
Add Comment
Please, Sign In to add comment