Title: Match Words With A Certain Ending
Slug: match_words_with_certain_ending
Summary: Match Words With A Certain Ending
Date: 2016-05-01 12:00
Category: Regex
Tags: Basics
Authors: Chris Albon

Source: Regular Expressions Cookbook

Preliminaries


In [1]:
# Load regex package
import re

Create some text


In [2]:
# Create a variable containing a text string
text = 'Capitalism, Communism, Neorealism, Liberalism'

Apply regex


In [3]:
# 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


Out[3]:
['Capitalism', 'Communism', 'Neorealism', 'Liberalism']