0Pricing
Python Academy · Lesson

Using re.match, re.search, re.findall

Apply core re functions to find patterns in strings.

Using re.match, re.search, re.findall is a free Python Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

The re module provides several functions for applying patterns to strings.

re.match() — Start of String

re.match(pattern, string) matches only at the BEGINNING of the string. Returns a Match object or None.
import re
print(re.match(r'\d+', '123abc'))  # match
print(re.match(r'\d+', 'abc123'))  # None

re.search() — Anywhere

re.search(pattern, string) finds the FIRST match anywhere in the string. Returns Match or None.
import re
m = re.search(r'\d+', 'abc 123 def')
if m: print(m.group())

re.findall() — All Matches

re.findall(pattern, string) returns a list of all non-overlapping matches as strings.
import re
print(re.findall(r'\d+', 'one 1 two 22 three 333'))

re.finditer() — Iterator

re.finditer() is like findall() but returns an iterator of Match objects — memory-efficient for large strings.
import re
for m in re.finditer(r'\d+', 'x1 y22 z333'):
    print(m.group(), m.start(), m.end())

Match Object Methods

m.group() returns the matched string. m.start() and m.end() return positions. m.span() returns (start, end).
import re
m = re.search(r'(\w+)@(\w+)', 'send to user@example')
if m:
    print(m.group(), m.group(1), m.group(2))

re.fullmatch()

re.fullmatch(pattern, string) requires the pattern to match the ENTIRE string.
import re
print(re.fullmatch(r'\d+', '123'))    # match
print(re.fullmatch(r'\d+', '123abc'))  # None

re.split()

re.split(r'\s+', string) splits on the pattern. r'[,;\s]+' splits on commas, semicolons, and spaces.
import re
print(re.split(r'[,;\s]+', 'one,two; three four'))

re.sub() for Replacement

re.sub(pattern, repl, string) replaces matches. repl can be a string or a function.
import re
print(re.sub(r'\d+', 'NUM', 'I have 3 cats and 12 dogs'))

Compiled Patterns

pattern = re.compile(r'\d+') compiles once. Use pattern.findall(), .search(), etc. — faster for repeated use.
import re
digits = re.compile(r'\d+')
print(digits.findall('abc 123 def 456'))

Flags

re.IGNORECASE (re.I), re.MULTILINE (re.M), re.DOTALL (re.S) modify matching behavior.
import re
print(re.findall(r'hello', 'HELLO World', re.IGNORECASE))

Quick Check

What is the difference between re.match() and re.search()?

Recap

re.match (start only), re.search (first anywhere), re.findall (all as list), re.finditer (all as Match objects), re.sub (replace), re.compile (reuse pattern).

Keep Going

Great work! The next lesson awaits.

Frequently asked questions

Is the “Using re.match, re.search, re.findall” lesson free?

Yes — the full text of “Using re.match, re.search, re.findall” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.

What will I learn in “Using re.match, re.search, re.findall”?

Apply core re functions to find patterns in strings. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Python Academy?

No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Using re.match, re.search, re.findall” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Python Academy lesson?

Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Regex Fundamentals
  2. Using re.match, re.search, re.findall
  3. Groups and Named Captures
  4. re.sub, Flags, and Compiled Patterns
← Back to Python Academy