Regular expressions, or RegEx for short, are a powerful and quick way to define search patterns on text. Using RegEx can initially feel like a strange cryptic language, and many might argue it is.
RegEx is its own separate language and doesn't have anything to do with Python. But Python supports the use of RegEx through the re module in its standard library.
Note: RegEx is not a core Python topic, and depending on what you do with Python, you might not need it at all. Use the resources in this lesson to train to delve into this topic as deeply as it feels helpful to you. Learning opportunities are vast out in the wild, and it's important to train how detailed you want to learn something, depending on what you need it for.
How to Learn RegEx
Having a basic grasp on using RegEx can, however, give you superpowers when interacting with text:
Don't try to learn RegEx all at once. In fact, don't even aim to learn the syntax of the language at all when you're getting started. Follow an online RegEx tutorial and try to grasp the idea of what you can use this language for. There are great online playgrounds, such as RegEx101 or RegExr that you can always refer to if you need to build a RegEx pattern to find just what you're looking for in your text:
Like with so many things in programming, the most important takeaway is that you know RegEx exists, what they are useful for, and where you can find the tools to work with them.
PyCon RegEx Tutorial
Trey Hunner created a great RegEx tutorial at PyCon 2017 that you can watch and work through to get that awareness and start with using RegEX.
The tutorial has good documentation, a video recording from the PyCon talk, as well as exercises.
Why Use RegEx
Some familiarity with RegEx can supercharge your ability to work with text, which can be helpful if you're dealing with large amounts of written content, such as what you might receive from scraping websites.
Additional Resources
- Wikipedia: Regular expressions
- Official Python Documentation:
re— Regular expression operations - Official Python Tutorial: RegEx
- PyCon2017 Tutorial: RegEx Training
- RegEx Playground: RegEx101
- RegEx Playground: RegExr
Summary: What is Python RegEx
- RegEx stands for regular expressions
- RegEx is a formal language that you can use to match patterns in text
- Many programming languages, including Python, have support for using RegEx
reis the module in Python's standard library that provides RegEx support