Reputation: 245
I have a string in python
x = "orange".
The program accepts an input string.If the input string matches the wildcard pattern of x, then I perform some operation. The pattern is
pattern = "#.orange.*"
# (hash) - can substitute for zero or more words.
*(star) - can substitute for exactly one word.
So, if my input string is "quick.orange.rabbit", then it should match. Can anyone help me how to do this in python?? I have tried -
import re
input_str = input("enter string")
pattern = #.orange.*
p = re.compile(pattern)
if p.match(input_str):
# do something
Thanks in advance.
Upvotes: 0
Views: 1954
Reputation: 106
Not sure if I'm understanding the Regex idea correctly, but if I'm correct, you want to match the following:
(Zero or more words).orange.(Exactly one word)
Now I do not know if the words in the hash are seperated by spaces, periods or are just words after each other, but here are the following cases:
Hash words separated by spaces
pattern = r"(?P<hash>(\w+|\s)*)\.(?P<orange>orange)\.(?P<star>\w+)"
Hash words seperated by periods
pattern = r"(?P<hash>(\w+|\.)*)\.(?P<orange>orange)\.(?P<star>\w+)"
Hash words just after each other
pattern = r"(?P<hash>(\w*))\.(?P<orange>orange)\.(?P<star>\w+)"
If you don't want to have the (?P<>) syntax, you can also just do the following
pattern = r"(\w+|\s)*\.orange\.\w+"
Which will match against your string and tell you if it is a match, but you then can't do
match.group('hash')
To get the hash words.
Upvotes: 1