Let’s say that my program receives an input such as a string of characters that has any type of character. For example, ‘Bob’s Bagel Shop’. Then it gets another string that says ‘Fred’s Bagel Store’. How can I use regular expressions or some other module in python to compare these and have my program tell me if at least 5 (or any number I want) of the characters are the same anywhere in the string, but all in the same order, such as the word ‘Bagel’?
Thanks.
There’s a Python standard library class
difflib.SequenceMatcherthat will help to solve your problem. Here’s a code sample:Result:
The result shows that both string has equal substring with 13 characters length (starting from 3-rd char in first string and 4-th char in second string).
You can use this match result object to get its fields as values: