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
In [1]:
# Load regex package
import re
In [2]:
# Create a variable containing a text string
text = 'Capitalism, Communism, Neorealism, Liberalism'
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]: