COMP1002 – Advanced Python
Lecture 6
Regular Expressions
Spring 2025
Regular Expressions
• In computing, a regular expression, also referred to as “regex” or
“regexp”, provides a concise and flexible means for matching strings of
text, such as particular characters, words, or patterns of characters.
• A regular expression is written in a formal language that can be
interpreted by a regular expression processor.
• Really clever “wild card” expressions for matching and parsing strings
[Link]
Regular Expression Quick Guide
^ Matches the beginning of a line
$ Matches the end of the line
. Matches any character
\s Matches whitespace
\S Matches any non-whitespace character
* Repeats a character zero or more times
*? Repeats a character zero or more times (non-greedy)
+ Repeats a character one or more times
+? Repeats a character one or more times (non-greedy)
[aeiou] Matches a single character in the listed set
[^XYZ] Matches a single character not in the listed set
[a-z0-9] The set of characters can include a range
( Indicates where string extraction is to start
) Indicates where string extraction is to end
[Link]
The Regular Expression Module
• Before you can use regular expressions in your program, you
must import the library using “import re”
import re
regex = r"this is a regex pattern"
• For the most part, regex patterns are expressed with raw string
notation (hence, the preceding r character).
The Regular Expression Module
• You can use [Link]() to see if a string matches a regular
expression, similar to using the find() method for strings
• [Link](pattern, string, flags=0)
• You can use [Link]() to extract portions of a string that
match your regular expression, similar to a combination of
find() and slicing: var[5:10]
The Regular Expression Module
• The [Link]() method returns a matching character pattern
at the beginning of a given string.
• The [Link]() method replaces matching substrings with a new
string for all occurrences, or a specified number.
Example
Using [Link]() Like find()
handle = open('[Link]')
for line in handle:
line = [Link]()
if [Link]('From:') >= 0:
print(line)
import re
handle = open('[Link]')
for line in handle:
line = [Link]()
if [Link]('From:', line):
print(line)
Using [Link]() Like startswith()
handle = open('[Link]')
for line in handle:
line = [Link]()
if [Link]('From:'):
print(line)
import re
handle = open('[Link]')
for line in handle:
line = [Link]()
if [Link]('^From:', line):
print(line)
Wild-Card Characters
• The dot character matches any character
• If you add the asterisk character, the character is “any number of times”
X-Sieve: CMU Sieve 2.3
X-DSPAM-Result: Innocent
X-DSPAM-Confidence: 0.8475
X-Content-Type-Message-Body: text/plain
Many times
Match the start of the line
^X.*:
Match any character
Fine-Tuning Your Match
• Depending on how “clean” your data is and the purpose of your
application, you may want to narrow your match down a bit
X-Sieve: CMU Sieve 2.3
X-DSPAM-Result: Innocent
X-: Very Short
X-Plane is behind schedule: two weeks
One or more
Match the start of the line
times
^X-\S+:
Match any non-whitespace character
Matching and Extracting Data
• [Link]() returns a True/False depending on whether the string
matches the regular expression
• If we actually want the matching strings to be extracted, we use
[Link]()
import re
[0-9]+ x = 'My 2 favorite numbers are 19 and 42'
y = [Link]('[0-9]+',x)
print(y)
One or more digits Output: ['2', '19', '42']
Matching and Extracting Data
When we use [Link](), it returns a list of zero or more sub-strings
that match the regular expression
import re
x = 'My 2 favorite numbers are 19 and 42'
y = [Link]('[0-9]+',x)
print(y)
Output: ['2', '19', '42']
y = [Link]('[AEIOU]+',x)
print(y)
Output: []
Warning: Greedy Matching
• The repeat characters (* and +) push outward in both directions (greedy)
to match the largest possible string
import re
x = 'From: Using the : character'
y = [Link]('^F.+:', x)
print(y)
Output: ['From: Using the :']
Last character in
First character in the the match is a :
match is an F
^F.+:
One or more characters
Non-Greedy Matching
• Not all regular expression repeat codes are greedy! If you add a ?
character, the + and * chill out a bit...
import re
x = 'From: Using the : character'
y = [Link]('^F.+?:', x)
print(y)
Output: ['From:']
Last character in
First character in the the match is a :
match is an F
^F.+?:
One or more characters but
not greedy
Fine-Tuning String Extraction
You can refine the match for [Link]() and separately determine
which portion of the match is to be extracted by using parentheses.
x = "From [Link]@[Link] Sat Apr 29 09:17:16 2023"
y = [Link]('\S+@\S+',x)
print(y)
Output: ['[Link]@[Link]']
\S+@\S+
At least one non-
whitespace character
Fine-Tuning String Extraction
Parentheses are not part of the match - but they tell where to start and
stop what string to extract.
x = "From [Link]@[Link] Sat Apr 29 09:17:16 2023"
y = [Link]('\S+@\S+',x)
print(y)
Output: ['[Link]@[Link]']
^From (\S+@\S+)
y = [Link]('^From (\S+@\S+)',x)
print(y)
Output: ['[Link]@[Link]']
String Parsing Examples
line = "From [Link]@[Link] Sat Apr 29 09:17:16 2023"
words = [Link]()
print(words)
Output: ['From', '[Link]@[Link]', 'Sat', 'Apr',
'29', '09:17:16', '2023']
email = words[1]
print(email)
Output: [Link]@[Link]
pieces = [Link]('@')
print(pieces[1])
Output: [Link]
The Regex Version
import re
line = "From [Link]@[Link] Sat Apr 29 09:17:16 2023"
y = [Link]('@([^ ]*)', line)
print(y)
Output: ['[Link]']
Match non-blank character
'@([^ ]*)'
Look through the string
Match many of them
until you find an at sign
The Regex Version
import re
line = "From [Link]@[Link] Sat Apr 29 09:17:16 2023"
y = [Link]('@([^ ]*)', line)
print(y)
Output: ['[Link]']
'@([^ ]*)'
Extract the non-blank characters
Escape Character
If you want a special regular expression character to just behave normally
(most of the time) you prefix it with '\'
import re
x = 'We just received $10.00 for cookies.'
y = [Link]('\$[0-9.]+',x)
print(y)
At least one or more
Output: ['$10.00']
\$[0-9.]+
A real dollar sign A digit or period
Exercise1
Write a program that extracts the number from each of the lines using a
regular expression and the findall() method. Compute the average of
the numbers and print out the average as an integer.
Sample file content
sample numbers are 15 and 17
9 is an odd number
12 is an even number
Sample program output
count of the numbers: 4
sum: 53
average: 13
Exercise1 - Solution
Write a program that extracts the number from each of the lines using a
regular expression and the findall() method. Compute the average of
the numbers and print out the average as an integer.
import re
file_handle = open("[Link]", "r")
sum = 0
count = 0
for line in file_handle:
line = [Link]()
number = [Link]('[0-9]+', line)
for n in number:
count += 1
sum += int(n)
print("count of the numbers:", count)
print("sum:", sum);
print("average:", sum//count)
Exercise2
Write a Python function that counts the number of occurrences of a
specific word in a given text using regular expressions.
Exercise2 - Solution
import re
def word_count(text, word):
# Using [Link] to find all occurrences of the word
matches = [Link](word, text, flags=[Link])
# Counting the number of matches
count = len(matches)
return count
text = "This is a sample text. This text contains the word sample
multiple times, including Sample and SAMPLE."
word = "sample"
print(f"The word '{word}' occurs {word_count(text, word)} times in
the text.")
References
• Slides are adapted from
• [Link]
• [Link] (
• Python for Everybody, Exploring Data Using Python 3, Dr. Charles R.
Severance