I have a string of words and I would like to return a word if a part of it matches. For e.g.
str = "welcome to the sunshine hotel"
j= re.search(r'sun',str).group(0)
print(j)
the output is : sun
what changes do I make to get the output as 'sunshine' ? Thank you
CodePudding user response:
This approach does not use regexes:
s = "welcome to the sunshine hotel"
j = next((w for w in s.split() if "sun" in w), None)
CodePudding user response:
j= re.search(r'sunshine',str).group(0)
In case you mean anything that starts with sun. Then:
j= re.search(r'sun(.*)',str).group(0)
Of course need to be careful for corner cases such as when followed by a period etc. Some website like this https://regex101.com/ might help you with your regex syntax.