Python @ DjangoSpin

PyPro #84 Match a word at the end of a string ending with an optional punctuation mark

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

Match a word at the end of a string in Python

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.


See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply