Search⌘ K
AI Features

I Know, Let’s Use Regular Expressions!

Explore how to use regular expressions in Python to detect specific character patterns and modify strings. Understand matching rules, substitutions, and syntax nuances to create functions that handle linguistic rules such as pluralization. This lesson helps improve string manipulation skills with practical regex applications.

We'll cover the following...

So you’re looking at words, which, at least in English, means you’re looking at strings of characters. You have rules that say you need to find different combinations of characters, then do different things to them. This sounds like a job for regular expressions!

Python 3.5
import re
def plural(noun):
if re.search('[sxz]$', noun): #①
return re.sub('$', 'es', noun) #②
elif re.search('[^aeioudgkprt]h$', noun):
return re.sub('$', 'es', noun)
elif re.search('[^aeiou]y$', noun):
return re.sub('y$', 'ies', noun)
else:
return noun + 's'

① This is a regular expression, but it uses a syntax you didn’t see in Regular Expressions. The square brackets mean “match exactly one of these characters.” So [sxz] means “s, or x, or z”, but only one of them. The $ should be familiar; it matches the end of string. Combined, this regular expression tests whether noun ends with s, x, or z.

② This re.sub() function performs regular expression-based string substitutions.

Let’s look at regular expression substitutions in more detail.

Python 3.5
import re
print (re.search('[abc]', 'Mark')) #①
#<_sre.SRE_Match object at 0x001C1FA8>
print (re.sub('[abc]', 'o', 'Mark')) #②
#Mork
print (re.sub('[abc]', 'o', 'rock')) #③
#rook
print (re.sub('[abc]', 'o', 'caps')) #④
#oops

① Does the string Mark contain a, b, or c? Yes, it contains a ...