100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Python

Regular Expressions in Python

Pattern matching and text processing using Python's re module -- syntax, key functions, and common pitfalls.

Concurrency & Interview PrepIntermediate14 min readJul 7, 2026
Analogies

1. Introduction

Regular expressions (regex) are patterns used to search, match, validate, and extract text. Python's built-in re module implements a full regex engine, letting you check whether a string matches a pattern, pull out substrings, split text on complex delimiters, or perform pattern-based find-and-replace -- far more powerful than plain string methods like find() or replace() for anything beyond exact substrings.

🏏

Cricket analogy: Regex is like a scout's detailed checklist for spotting a specific bowling action across hours of footage -- far more powerful than simply skimming for a player's name (find()), letting you extract every no-ball pattern at once.

2. Syntax

python
import re

pattern = r"\d+"
result = re.findall(pattern, "Order 42 has 7 items")
print(result)

3. Explanation

re.match() only checks for a match at the beginning of the string; re.search() scans the whole string and returns the first match anywhere; re.fullmatch() requires the entire string to match the pattern exactly. Quantifiers can be greedy (*, +, {m,n}) which match as much text as possible, or non-greedy/lazy (*?, +?, {m,n}?) which match as little as possible.

🏏

Cricket analogy: re.match() is like checking only the very first ball of an over for a no-ball, re.search() scans the whole over for any no-ball, and re.fullmatch() demands every ball in the over be a no-ball; a greedy quantifier is like a batsman going for every run possible, while lazy takes just a single run.

Common metacharacters include \d (digit), \w (word character: letters, digits, underscore), \s (whitespace), . (any character except newline), ^/$ (start/end of string), [] (character class), () (grouping/capturing), and | (alternation). re.findall() returns all non-overlapping matches as a list, re.sub() performs pattern-based replacement, and re.compile() pre-compiles a pattern into a reusable Pattern object for efficiency when the same regex is used repeatedly.

🏏

Cricket analogy: \d is like matching only jersey numbers, \w matches any player-name character, \s matches gaps between names on a team sheet, and () groups a player's stats together; re.findall() lists every six hit in an innings, re.sub() replaces a player's old name in the record book, and re.compile() pre-builds a reusable scouting filter for the whole season.

Always write regex patterns as raw strings (e.g., r"\d+" instead of "\\d+") to avoid Python's own string-escaping rules conflicting with the regex engine's escaping rules. Without the r prefix, backslashes in patterns like \d or \s can be misinterpreted or require doubling up.

4. Example

python
import re

text = "Contact: john.doe@example.com or jane_smith99@test.org"
pattern = r"[\w.]+@[\w.]+"

matches = re.findall(pattern, text)
print(matches)

m = re.search(r"(\w+)@(\w+)", text)
print(m.group(1))
print(m.group(2))

5. Output

text
['john.doe@example.com', 'jane_smith99@test.org']
doe
example

6. Key Takeaways

  • re.match checks only the start of a string; re.search scans the whole string; re.fullmatch requires the entire string to match.
  • Always use raw strings (r"...") for regex patterns to avoid escaping headaches.
  • Greedy quantifiers (*, +) match as much as possible; adding ? (*?, +?) makes them non-greedy.
  • re.findall returns a list of all matches; re.sub performs pattern-based substitution.
  • re.compile() pre-compiles a pattern for reuse and better performance in loops.
  • Common classes: \d digits, \w word characters, \s whitespace, . any character except newline.

Practice what you learned

Was this page helpful?

Topics covered

#Python#PythonProgrammingStudyNotes#Programming#RegularExpressionsInPython#Regular#Expressions#Syntax#Explanation#StudyNotes#SkillVeris