Let suppose we have a list i python of 35000 values like:
a = ['235', '2589', '25896']
and string to match:
str = '258963548'
str2 = '258954213'
str3 = '258659652'
Now I want to match these strings to list to find longest match. result for first string will be 25896 while second match will return 2589 and finally last string will failed to match.
I have used regular expression to solve this issue but it's taking long as I have around 50 sets of list and around 200 strings to match each list.
here is my code:
def Matchit(str,b = []):
pattern = re.compile("(?P<mt>\S*)\S*\s+(?P=mt)")
ln = 0
res = -1
for a in b:
match = pattern.match(str + ' ' + a).group('mt')
if (len(match)>ln):
ln = len(match)
if(ln>2):
res = b[a]
return res
Any help will be appreciated.