0

I'm setting up a script and I need to return a bool when a string is detected in another string.

I've tried to use the function str.find() but it doesn't fit with what I want to obtain.

str1 = 'test*'
str2 = 'test12345'

How can I return a bool if 'test' is in str2 by taking in consideration that '*' is everything after the string 'test'.

I've tried to use .find() as:

str2.find(str1.replace('*','')

I'm trying to search a easier way to do that.

Thanks in advance.

Duplicated?

The question has been marked as duplicated but the topic linked doesn't give an answer with a string containing a * symbol.

Bando
  • 1,223
  • 1
  • 12
  • 31

3 Answers3

0

So if you are only planning on using one search string yes the suggested answers will work but if you format could expand to "test\*hat\*jack" then you should split the string then insure that all parts of the split string are in the master string.

Example:

keywords = "test\*hat\*jack"
masterStr = "I put my hat on jack as a test"

flag = True


for entry in keywords.split("*"):
    if entry not in masterStr:
        flag = False

return flag

Ideally your search keywords should be stored in a list rather than a deliminated string.

Bando
  • 1,223
  • 1
  • 12
  • 31
0

In fact, with the help of few users, the answer of my question is:

str1 = 'test*'
str2 = 'test12345'

str.startswith(str1.replace('*',''))

It will return True if str2 begins by str1 without the * symbol.

Bando
  • 1,223
  • 1
  • 12
  • 31
0
str1 = "test *"
str2 = "test11234"

strls1 = str1.split(" ") #convert the string to a list
strls2 = str2.split(" ")

def check(str1, str2):
for x in str1:
  for y in str2:
     if x == y:
        return True

check(strls1, strls2)

Should work if the rest of string 1 is separated by spaces, its a bit crude though.

Here is a page that shows how to use string.split

Kaito
  • 183
  • 11