My goal is to get the last word of a string, no matter what the word is.
With a lot of trials and error I got kinda lucky with the following code, because instead of \w+ I tried \W+ and got a result I could work with.
But my actual code (the one you don't see here) is kinda messy, so my question is; What is the right compile regex to use to get the last word, or two words?
Thanks in advance!
import re
var = ' hello my name is eddie '
r = re.compile(r'\S+\W+$')
r2 = r.findall(var)
print(r2)
#result ['eddie ']
(\S+)\s*$regex101.com/r/BcF2vb/1var.strip().split(" ")[-1], and[-2:]if you need 2 words.