You can do this, in order to test whether comment_in_movie contains all characters from user_comment in the correct order. Additional characters that are missing from user_comment are permitted anywhere inside comment_in_movie, and the loop over the characters of comment_in_movie will just carry on until it finds matching characters again. Provided that the end of user_comment is reached before running out of characters in comment_in_movie, that is considered a match.
user_comment = "Hobbit 2013:Bad Movie"
comment_in_movie = "Hobbit 2013 [email protected]:Bad Movie"
i = 0
for c in comment_in_movie:
if user_comment[i] != c:
continue
i += 1
if i == len(user_comment):
found = True
break
else:
found = False
if found:
print("found")
The above code makes no assumptions whatsoever regarding permitted places in which missing characters may occur. In some sense, this is more flexible because it checks that all characters are present, while not needing any advance knowledge of what delimiter(s) to use to split the string. However, it might be for example that gaps in the middle of a word should not be acceptable (e.g. if user_comment contained Hit instead of Hobbit).
For this reason, an alternative version is given below which is based on looking for whole words. All the words in user_comment now have to appear in comment_in_movie, again in the correct order, and non-word characters e.g. punctuation are simply ignored. The logic is exactly the same, except that we loop over words in lists, instead of characters in strings. So for example, "Hobbit 2013,Bad Movie" would be "found", without requiring that the comma is contained in comment_in_movie, but "Hit 2013:Bad Movie" would not be "found".
import re
user_comment = "Hobbit 2013:Bad Movie"
comment_in_movie = "Hobbit 2013 [email protected]:Bad Movie"
user_comment_words = re.findall("\w+", user_comment)
comment_in_movie_words = re.findall("\w+", comment_in_movie)
i = 0
for w in comment_in_movie_words:
if user_comment_words[i] != w:
continue
i += 1
if i == len(user_comment_words):
found = True
break
else:
found = False
if found:
print("found")
incheck if the entirety of string 1 is in string 2. in your case there is SOME overlap, but clearly user_comment contains parts not in comment_in_movie. what are you trying to achieve? if there is always going to be a colon you can always slice the string