Skip to main content
Module 8 · Lesson 4 of 432/36 in the course~16 min
Module lessons (4/4)

re module: regex in Python

Lesson preview5 hands-on exercises readyPython Course · Module 8 — Essential standard library

re.search / re.match / re.findall / re.sub, flags, raw strings r"...", and named groups.

What you'll learn in this lesson

  • Writing safe patterns in Python using raw string r"..." to avoid double backslashes.
  • Choose the right function from re.search, re.match, re.fullmatch, re.findall, re.finditer and re.sub.
  • Extract structured substrings with positional capturing groups and named groups (?P<name>...).
  • Prefill repeating expressions with re.compile() and apply flags like re.IGNORECASE and re.MULTILINE.

Overview

The re module brings the power of regular expressions directly to Python. The number one rule is to write the patterns as raw string (r"\d+"): in a normal string, in fact, Python interprets some escapes before the regex even sees them (\b would become ASCII backspace, \n would become newline!), while r"..." delivers every character intact…

Always choose the narrowest operation for your purpose: avoid re.findall on giant texts when you only need the first occurrence (re.search) or a lazy stream (re.finditer).

Sections covered in this lesson

  1. 11. Raw string, re.search and walrus operator
  2. 22. re.findall, capture groups and re.finditer
  3. 33. Named groups (?P<name>...) and substitutions with function
  4. 44. re.compile and main flags
  5. 5Check for understanding
  6. 6Test yourself
Pro Lesson · Module 8 — Essential standard library

Unlock "re module: regex in Python" and the full Python Course curriculum

re.search / re.match / re.findall / re.sub, flags, raw strings r"...", and named groups.

Unlock all 5 hands-on exercises in this lesson plus every advanced WebAssembly module with automated checks.

  • Instant access to all advanced modules in Python Course plus 17 more courses
  • Hands-on browser exercises with automated test suites and solutions
  • 14-day money-back guarantee processed via Paddle.com

14-day guarantee · Cancel anytimeModule 1, all 18 interactive games, and cheatsheets are always free.