Regexes for Normal People (Like Me) in Python (1/3)
Part 1: Understanding the Basics of Regular Expressions in Python
Regexes are hard and boring? Not if you know the rules. I had a colleague in my early job and I used to ask myself: how did he know what to do with regexes — they are super hard. Well, now I know.

First thing first…
In Python, we need the re module to start working with regexes. We can use re.compile() or directly call re with some methods. Which methods? Let's discover them.
- match(): Determine if the RE matches at the beginning of the string.
- search(): Scan through a string, looking for any location where this RE matches.
- findall(): Find all substrings where the RE matches, and returns them as a list.
- finditer(): Find all substrings where the RE matches, and returns them as an iterator.
I find search and findall to be the most useful methods. match and finditer are more suitable for special cases. We don't always need to work with iterators, and match is specifically for searching from the beginning.
Meta characters
We need these meta tags to make a complex pattern, which helps us easily make a blueprint for the string we need to match.
- . Any character (except newline character)
hu..n - ^ Starts with
^hello - $ Ends with
world$ - * Zero or more occurrences
py* - + One or more occurrences
py+ - { } Exactly the specified number of occurrences
py{2} - [] A set of characters
[a-z] - \ Signals a special sequence (can also be used to escape special characters)
\d - | Either or
python|javascript - ( ) Capture and group
Quantifiers
We covered some of these in the meta characters. Quantifiers are simply a way of specifying how many times a pattern should be matched.
This is particularly useful, for instance, when allowing variations in formatting, such as typing a phone number with or without whitespace, like +420 111222333 or +420111222333.
- * : 0 or more
- + : 1 or more
- ? : 0 or 1, used when a character can be optional
- {4} : exact number
- {4,6} : range numbers (min, max)
We have everything we need to see some examples in action, which we will do in the next part. Of course, there is much more to learn about regexes, but it doesn’t make sense to try to learn everything at once. We might forget half of it, and we won’t use the other half.
It’s better to start small, understand the basics, and then move on to more complex patterns. This is how we will continue in the next part.
If you enjoyed the read and want to be part of our growing community, hit the follow button, and let’s embark on a knowledge journey together.
Your feedback and comments are always welcome, so don’t hold back!
Stackademic
Thank you for reading until the end. Before you go:
