Python @ DjangoSpin

PyPro #60 Extract words starting with 'a', 'd' or 's'

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

Extract words starting with a given letter in Python

Extract words starting with a given letter in Python

Extract words starting with 'a', 'd' or 's'. Write a Regular Expression in Python to extract all words from a string starting with a, d or s. 

Extract words starting with 'a', 'd' or 's'

import re

sourceString = 'apple ball cat dog eagle sea sort'

print( re.findall(r'\b[ads]\w+\b', sourceString) )			# ['apple', 'dog', 'sea', 'sort']

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