Home > Net >  Matching a string if it contains all words of a list in python
Matching a string if it contains all words of a list in python

Time:12-01

I have a number of long strings and I want to match those that contain all words of a given list.

keywords=['special','dreams']
search_string1="This is something that manifests especially in dreams"
search_string2="This is something that manifests in special cases in dreams"

I want only search_string2 matched. So far I have this code:

if all(x in search_text for x in keywords):
   print("matched")

The problem is that it will also match search_string1. Obviously I need to include some regex matching that uses \w or or \b, but I can't figure out how I can include a regex in the if all statement.

Can anyone help?

CodePudding user response:

you can use regex to do the same but I prefer to just use python.

string classes in python can be split to list of words. (join can join a list to string). while using word in list_of_words will help you understand if word is in the list.

keywords=['special','dreams']
found = True
for word in keywords:
    if not word in search_string1.split():
        found = False

CodePudding user response:

Could be not the best idea, but we could check if one set is a part of another set:

keywords = ['special', 'dreams']

strs = [
  "This is something that manifests especially in dreams",
  "This is something that manifests in special cases in dreams"
]

_keywords = set(keywords)
for s in strs:
  s_set = set(s.split())
  if _keywords.issubset(s_set):
    print(f"Matched: {s}")
  • Related