Reputation: 85
I have this:
incompleted_string1 = "Thom"
incompleted_string2 = "s Mueller naive"
entire_string = 'Thom.s Mueller naive' # <= dot means any char!!! I dont know which char is it
pattern = "mas M"
I would like to know if "mas M" if present inside entire_string. I do not care if "."
is equal to "a" or something else. I cannot change the pattern string!
re.findall("mas M", entire_string)
This returns []
I'd like to have "mas M"
but True
will be enough
Thank you for your help
Upvotes: 2
Views: 63
Reputation: 8273
The another approach could be to have all the possible combinations of pattern
bool(re.search(pattern + "|"+ "|".join([pattern[0:i] + '.' + pattern[i+1:] for i in range(len(pattern))]), entire_string))
Upvotes: 1
Reputation: 626903
You can replace each char in the pattern
with [
+ this char + .
+ ]
:
bool(re.search("".join([f"[{x}.]" for x in pattern]), entire_string))
The pattern will look like [m.][a.][s.][ .][M.]
here, and each can match either the corresponding letter or a dot. See the regex demo.
See the Python demo:
import re
incompleted_string1 = "Thom"
incompleted_string2 = "s Mueller naive"
entire_string = 'Thom.s Mueller naive' # <= dot means any char!!! I dont know which char is it
pattern = "mas M"
print (bool(re.search("".join([f"[{x}.]" for x in pattern]), entire_string)) )
# => True
Upvotes: 2