Reading Time: 1 minutes
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.