Reading Time: 1 minutes
Match a word at the end of a string in Python
Write a Regular Expression in Python to match a word at the end of a string ending with an optional punctuation mark.
import re sourceString = 'this a a sentence' print( re.findall( '\w+\S*$', sourceString) ) # ['sentence'] sourceString = 'this a another sentence.' print( re.findall( '\w+\S*$', sourceString) ) # ['sentence.']
To learn more about Regular Expressions in Python, click here.