Philipp
Philipp

Reputation: 65

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

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?

Upvotes: 2

Views: 929

Answers (3)

Philipp
Philipp

Reputation: 65

Axe319's comment works and is closest to my original question of how to solve the problem using regex. To quote the solution again:

all(re.search(fr'\b{x}\b', search_text) for x in keywords)

Thanks to everyone!

Upvotes: 1

Reishin
Reishin

Reputation: 1954

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}")

Upvotes: 2

Avizipi
Avizipi

Reputation: 498

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

Upvotes: 1

Related Questions