Match Words With A Certain Ending

Regular Expressions Cookbook


# Load regex package
import re

Create some text

# Create a variable containing a text string
text = 'Capitalism, Communism, Neorealism, Liberalism'

Apply regex

# Find any word ending in 'ism'
re.findall(r'\b\w*ism\b', text)

# Specific:
# \b     - start of the word
# \w*    - a word of any length
# ism\b  - with 'ism'at the end
['Capitalism', 'Communism', 'Neorealism', 'Liberalism']