I was trying the python regular expression and was trying to get first and last word from the string using ^\w*
and \w*$
. But I am getting the following results:
>>> re.findall(r'^\w*', 'This is a test string')
['This']
#to get the last word
>>> re.findall(r'\w*$', 'This is a test string')
['string', '']
can someone explain how this regexp works and why i am getting the empty element after string (['string', '']
).
Note: It works with ^\w+
and \w+$
.