Regular Expressions in Python
Regular expressions (regex) are powerful patterns for matching and manipulating text. They're used for validation (emails, phone numbers), searching, extracting data, and cleaning text.
Python's re module provides full regex support. Patterns can look complex at first, but they're built from simple building blocks.
We'll start with basics and build up with lots of examples.
Importing re and Basic Patterns
Import re and use raw strings (r"") for patterns to avoid escape issues.
Common characters:
- . – any character (except newline)
- ^ – start of string
- $ – end of string
- * – 0 or more
- + – 1 or more
- ? – 0 or 1
- \d – digit
- \w – word character
- \s – whitespace
Main re Methods
search() – first match anywhere match() – match at start findall() – all matches as list sub() – replace matches
Groups and Capturing
Use () to capture parts of the match.
Flags for Advanced Matching
re.IGNORECASE, re.MULTILINE, re.DOTALL etc.
Practical Examples
Validate simple email:
Extract phone numbers:
Quick Quiz
Az anyagokat átnéztük és ellenőriztük, de hibák továbbra is előfordulhatnak. A tartalom kizárólag oktatási célt szolgál, ezért saját felelősségre használd, és szükség esetén ellenőrizd más forrásokkal is.
✨ Kérdezd Larát — a tanulási partnered
Fedezd fel a személyre szabott tanulási támogatást. Lara elmagyarázza az anyagot, összefoglalja a témákat és megválaszolja a kérdéseidet — az Go csomagtól elérhető.
Lara segít gyorsabban tanulni — kizárólag a ReadyTools Go, Plus és Max tagoknak.

