Regular Expressions in Python

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

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 matchessearch: Return a match object if there is a matchsplit: Return a list of split matchessub: Replace one or more matches with a stringThen 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.
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)
print(x)
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.
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 = re.search("he", string)
print(x)
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 = re.search("\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
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)
print(x)
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)
print(x)
Gives us:
['he', 'was close to hearing the rain']
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)
print(x)
Will give us the following result:
he-was-close-to-hearing-the-rain
Which is super useful for filenames for instance!
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter