0PricingLogin
Learn AI with Python · Lesson

re Module: search, match, findall, sub

re.search(), re.match(), re.findall(), re.sub(), re.compile() for performance.

The re Module

Python exposes regex through the built-in re module. The core functions are search, match, findall, and sub.

Knowing which to reach for is half the battle.

import re   # always import it first

re.search — Find Anywhere

re.search(pattern, text) scans the whole string for the first match and returns a Match object, or None if nothing matches.

import re

m = re.search("\d+", "order 42 shipped")
if m:
    print(m.group())   # "42"
    print(m.start())   # 6

All lessons in this course

  1. Regex Patterns and Character Classes
  2. re Module: search, match, findall, sub
  3. Capturing Groups and Named Groups
  4. Text Cleaning for AI with Regex
← Back to Learn AI with Python