You can simply use the guard feature along with capture:
>>>my_str = "iwill/predict_something"
>>>match my_str:
... case str(x) if 'predict' in x:
... print("match!")
... case _:
... print("nah, dog")
...
match!
Answer from hyp3rg3om3tric on Stack Overflowpython match case part of a string - Stack Overflow
Python match a string with regex - Stack Overflow
Python search strings with partial match
Comparing Strings
Videos
You can simply use the guard feature along with capture:
>>>my_str = "iwill/predict_something"
>>>match my_str:
... case str(x) if 'predict' in x:
... print("match!")
... case _:
... print("nah, dog")
...
match!
You could use the [*_] wildcard sequence capture pattern: https://peps.python.org/pep-0622/#sequence-patterns
def is_holiday(yourstring: str):
match yourstring.split():
case [*_, "holidays"]:
return True
case [*_, "workday"]:
return False
print(is_holiday("xmas holidays"))
Are you sure you need a regex? It seems that you only need to know if a word is present in a string, so you can do:
>>> line = 'This,is,a,sample,string'
>>> "sample" in line
True
The r makes the string a raw string, which doesn't process escape characters (however, since there are none in the string, it is actually not needed here).
Also, re.match matches from the beginning of the string. In other words, it looks for an exact match between the string and the pattern. To match stuff that could be anywhere in the string, use re.search. See a demonstration below:
>>> import re
>>> line = 'This,is,a,sample,string'
>>> re.match("sample", line)
>>> re.search("sample", line)
<_sre.SRE_Match object at 0x021D32C0>
>>>
Iโm trying to match a domain name with a list of organisations to assign the correct company name to that domain name.
So inputs are:
domain name
list of company names
What is the best way/library in Python to surface the best match?