Python Program
Write a program to print the following, Given a word W and pattern P, you need to check whether the pattern matches the given word.The word will only contain letters(can be Uppercase and Lowercase). The pattern can contain letters, ? and *.
? : Can be matched with any single letter.
* : Can be matched with any sequence of letters (including an empty sequence).
If the pattern matches with the given word, print True else False.
Sample Input1
3
Hello *l?
Hell He?ll
Hell ?*
Sample Output1
True
False
True
Sample Input1
3
Hello Hell*
Hell He*ll
Hell hell*
Sample Output1
True
True
False
import re
number_tests = int(input())
for _ in range(number_tests):
word, pattern = map(str, input().split(' '))
validator = ""
for value in pattern:
if value == "*":
validator = f"{validator}.*"
elif value == "?":
validator = f"{validator}."
else:
validator = f"{validator}[{value}]"
result = re.fullmatch(validator, word)
if result is None:
print("False")
else:
print("True")
Comments
Leave a comment