HOLIDAY SALE! Save 50% on Membership with code HOLIDAY50. Save 15% on Mentorship with code HOLIDAY15.

4) Web Scraping Lesson

Python RegEx

5 min to complete · By Martin Breuss

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.

Illustration of a lighthouse

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:

RegEx comic by xkcd https://xkcd.com/ and https://creativecommons.org/licenses/by-nc/2.5/ RegEx comic by @garbatokid https://twitter.com/garabatokid

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:

Screenshot of RegEx101 online RegEx playground showing a pattern and a match on the beginning of the text from the Regular expressions Wikipedia article

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.

Colorful illustration of a light bulb

Additional Resources

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
  • re is the module in Python's standard library that provides RegEx support