I have two different arrays populated with a number of string values and I would like to compare the two arrays and find the matches. The main problem is that the two strings I’m trying to find the match for are not exactly the same and may include symbols such as ‘-‘
For example I would like to match:
‘Cat-Mouse’ with the word cat
I’ve tried using the diff lib close_matches library but no matches were found.
I also tried using the intersection method were I would compare two sets of letters together but since the strings are not exactly the same this causes problems.
Is there any other way I can try?
CodePudding user response:
If you just need to know if a word/substring is in a string, then you don't need to care about everything else around that word. But you need to pay attention, your example is case sensitive.
two ways:
1)
s = "Cat-Dog"
search = "cat"
result = search in s.lower() # s.lower equals "cat-dog"
print(result)
True
- with regular Expression
import re
s = "Cat-Dog"
search = "cat"
if re.search(search, s, re.IGNORECASE): #True when there is a match
print('found a match')