Regular Expressions in Python


Exploring the possibilities for Regular Expressions in Python

30 May, 2021 · 3 min read

Regular Expressions are a touchpoint you find in most programming languages, and there will come a time in your developer life you will need them.

Today I’m going to check out how they work in Python.

Before we can use regular expressions in Python, we have to import the module. It does come with the standard package, so no pip install is needed.

import re

There are multiple functions we can use for the Regular Expressions, and they are:

  • findall: Return a list of all matches
  • search: Return a match object if there is a match
  • split: Return a list of split matches
  • sub: Replace one or more matches with a string

Then there is a whole set of characters and sequences available, which I won’t be diving deeper into. I would strongly suggest looking at a regex tool like Pythex.

Finding all results

Let’s start by looking at the findall function and how it works.

import re

string = "he was close to hearing the rain"
x = re.findall("he", string)

This example will look for all occurrences of he and will return a list that states:

['he', 'he', 'he']
# he, hearing, the

It will return an empty list if nothing is found.

Python Regular Expression Search function

Another great addition is the search for regular expressions. Let’s first see how it works based on the above example.

string = "he was close to hearing the rain"
x ="he", string)

The match object looks something like this:

<re.Match object; span=(0, 2), match='he'>

We can use the start and end functions on this result to get that position like this:

string = "he was close to hearing the rain"
x ="\s", string)
print("he word starting on position:", x.start(), "and ending on:", x.end())

Which will result in:

he word starting on position: 2 and ending on 3

Splitting regular expression results in Python

A quite common approach for RegEx is to split the results into a list. Let’s say we want to break all the spaces in a sentence:

string = "he was close to hearing the rain"
x = re.split("\s", string)

Gives us the following list:

['he', 'was', 'close', 'to', 'hearing', 'the', 'rain']

We can also give the number of splits we want to make, so we only need the first occurrence.

string = "he was close to hearing the rain"
x = re.split("\s", string, 1)

Gives us:

['he', 'was close to hearing the rain']

Replacing regular expression matches in Python

What we, however, want to do in some cases is replacing specific matches.

Let’s say we want to convert all spaces to dashes.

string = "he was close to hearing the rain"
x = re.sub("\s", '-', string)

Will give us the following result:


Which is super useful for filenames for instance!

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

Speedtest your connection in Python

21 Jun, 2021 · 2 min read

Speedtest your connection in Python

F-strings in Python

7 Jun, 2021 · 2 min read

F-strings in Python

Join 2100 devs and subscribe to my newsletter