Alvis矜
Alvis矜

Reputation: 133

Searching for keywords in a string efficiently

m = "a1 - a2 - a3" #stored in database 

n = "a4 - a5" #given by user

"-" is just a seperator I used while making the database to make things orderly

I have many strings like these and what I am trying to do is check whether n is in m.

if n.lower() in m.lower():

This is what I have right now.

m = "a - b - c" #example

n = "b - c" #given by user

This will return True and it seemed to me that everything worked out fine.

m = "a - b - c" #example

n = "c - b" #given by user

But I want this to return True too. Order shouldn't be concerned.

Tldr; I am trying to find multiple words/characters in a string but the order shouldn't matter.

def search(userString, dbString):
    userString.lower()
    dbString.lower()
    for i in dbS.split():
        for j in userS.split():
            if j in i:

I tried something like this but I really don't know how to proceed.

Upvotes: 1

Views: 50

Answers (1)

Tushar Kale
Tushar Kale

Reputation: 169

m = "a - b - c"
n = "c - b"
flag = False
array = n.split()
for elem in array:
    if elem not in m:
        flag = True
if flag is True:
    print("Not a sub string")

Upvotes: 1

Related Questions