To check whether any of the words in a list are in the string:
any(word in string for word in lst)
Example:
# construct list from the query by removing 'OR', 'AND'
query = "tom OR jerry OR desperate AND dan OR mickey AND mouse"
lst = [term for term in query.split() if term not in ["OR", "AND"]]
string = "cartoon dan character desperate"
print any(word in string for word in lst)
If you use re.search() as @jro suggested then don't forget to escape words to avoid collisions with the regex syntax:
import re
m = re.search("|".join(map(re.escape, lst)), string)
if m:
print "some word from the list is in the string"
The above code assumes that query has no meaning other than the words it contains. If it does then assuming that 'AND' binds stronger than 'OR' i.e., 'a or b and c' means 'a or (b and c)' you could check whether a string satisfies query:
def query_in_string(query, string):
for term in query.split('OR'):
lst = map(str.strip, term.split('AND'))
if all(word in string for word in lst):
return True
return False
The above could be written more concisely but it might be less readable:
def query_in_string(query, string):
return any(all(word.strip() in string for word in term.split('AND'))
for term in query.split('OR'))
Example
query = "tom OR jerry AND dan"
print query_in_string(query, "cartoon jerry") # -> False no dan or tom
print query_in_string(query, "tom is happy") # -> True tom
print query_in_string(query, "dan likes jerry") # -> True jerry and dan
If you want to reject partial matches e.g., 'dan' should not match 'danial' then instead of word in string you
could use re.search() and add '\b':
re.search(r"\b%s\b" % re.escape(word), string)
and/oroperators are boolean. Your expression boils down to `if True in "cartoon..":