-2

For example, I have the String "abcd", and I want all matches to be found in which at least 2 of those characters match, in the correct position. So, ab12, a1c2, 12cd, etc will all match because they contain at least 2 characters in the correct index from abcd.

I realize I could try doing it by /ab..|a.c.|a..d|.bc.|.b.d|..cd/g, but is there a better/simpler way to do this?

Thank you!!

Anon
  • 9

1 Answers1

1

You can easily accomplish this with the PyPi regex package.

See code working here

import regex

s = 'abcd'
a = ['ab12', 'a1c2', '12cd', '123d', 'abc4', 'abcd']

r = regex.compile('(?:'+regex.escape(s)+'){e<=2}')

for x in a:
    if(r.fullmatch(x)):
        print(x)

This uses fuzzy matching {e<=2} to identify strings that have 2 or fewer errors (insertion, substitution, deletion). You can instead specify {s<=2} for only substitutions if you'd like.

For list comprehension, you can replace the last three lines with the following:

print([x for x in a if(r.fullmatch(x))])
ctwheels
  • 21,901
  • 9
  • 42
  • 77