Python Friday #211: First Steps With Regular Expressions

I waited a long time to explore regular expressions, then they tend to get complicated in no time. But for some problems there is simply no way around. Let us explore the basics and how we can use them in our Python code.

This post is part of my journey to learn Python. You find the code for this post in my PythonFriday repository on GitHub.


No installation required

Regular expressions are part of the standard library of Python. Therefore, we do not need to install a specific package and can import the re module with this line:


Pythex: A Python regular expression editor

While writing regular expressions (short regex), we need fast feedback then it will take a few rounds until they work the way we want it. Tools like Pythex, a regular expression editor in the web, can answer immediately if our regex works or not. We can enter our sample text and then try our regular expressions until they match what we want to find.

Pythex lets you test your regular expressions in the web browser

While this is optional, I suggest you use a tool like Pythex to help you visualise the matches of your regular expressions. It makes working with them so much less cumbersome.


Find text parts (basic)

To figure out if our regex matches something anywhere in a text, we can use the function:

If the regex matches a part of the text, we get back a Match object with the first position:

To find all parts that match our regex, we can use the re.findall() function:

This gives us a list of all parts that match our regular expression:

If we want to get an iterator for all matches, we can use the function re.finditer():

To see the different matches, we can loop through our result:

If we only care about a match at the beginning of a text, we can use the re.match() function:

If our regex should match the whole text, we can use the re.fullmatch() function:

While these examples show us how the main functions in the re module work, they only scratch at the surface and are not of much use. That will change when we combine those basic functions with metacharacters, which will be the topic of next week’s post.


Substitute text parts with other text parts

The re.sub() function searches for our regex, replaces all matches and returns the text with the replaced parts:

If we want to know how many substitutions happened, we can use the re.subn() function instead:

This gives us a tuple with the result and the number of substitutions back.


Compile regular expressions

If we reuse the same regular expressions multiple times, we better compile it once and then work with that pre-compiled object to speed up our application:



With these basic parts of the re module we can already do a bit of useful work. However, the main benefit from regular expressions comes from metacharacters, that we will explore next week.

3 thoughts on “Python Friday #211: First Steps With Regular Expressions”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.