I am new to Python and still learning basic concepts of it. I was wondering if there is any easy way to get all the words from a sentence where my search string matches the substrings of any words.
sentence = "Hi this is just an example.com sentence"
search = ".com"
Output :
example.com
I tried to use re module but got stuck at point where I need to fetch the whole word. This is what I have tried which returns the whole sentence as output:
re.findall(f".*{search}*", sentence)
I wanted to know if I am using the right approach or if there is any other way to solve this.
Additional Question :
Using reference of John's answer, [w for w in sentence.split() if '.com' in w]
I am able to get the expected output.
As an additional requirement, if
sentence = "Hi this is just an Example.COM sentence"
I still want it to match with .com and return Example.COM
recould work but seems like overkill. You could just use[w for w in sentence.split() if '.com' in w]redirection so forgot the basics. Additional question : If the sentence is"Hi this is just an example.COM sentence"I still want it to match with.comand returnexample.COM