
Python
Regular expressions (regex) let you search, match, and extract text using patterns. They're powerful tools for text processing.
A regular expression is a pattern that describes text. Use the `re` module to work with regex in Python:
import re
# Simple pattern matching
text = "The phone number is 555-1234."
pattern = r"\d{3}-\d{4}" # Pattern: 3 digits, dash, 4 digits
match = re.search(pattern, text)
if match:
print(f"Found: {match.group()}") # Found: 555-1234Regex uses special characters to define patterns:
import re
# . matches any character except newline
re.search(r"c.t", "cat") # Match: 'cat'
re.search(r"c.t", "cut") # Match: 'cut'
# \d matches any digit
re.search(r"\d", "abc123") # Match: '1'
# \w matches word characters (letters, digits, underscore)
re.search(r"\w+", "hello") # Match: 'hello'
# \s matches whitespace
re.search(r"\s", "hello world") # Match: space
# ^ matches start of string
re.search(r"^Hello", "Hello World") # Match
# $ matches end of string
re.search(r"World$", "Hello World") # MatchControl how many times a pattern repeats:
import re
# * means 0 or more
re.findall(r"a*", "aaa baa") # ['aaa', '', 'aa', '']
# + means 1 or more
re.findall(r"a+", "aaa baa") # ['aaa', 'aa']
# ? means 0 or 1
re.findall(r"colou?r", "color colour") # ['color', 'colour']
# {n} means exactly n
re.findall(r"\d{3}", "123 45 678") # ['123', '678']
# {n,m} means n to m
re.findall(r"\d{2,3}", "12 345 6789") # ['12', '345', '67']| Pattern | Meaning | Example |
|---|---|---|
| `.` | Any character | `a.c` matches "abc", "adc" |
| `\d` | Any digit | `\d+` matches "123" |
| `\w` | Word character | `\w+` matches "hello" |
| `\s` | Whitespace | `\s+` matches spaces/tabs |
| `^` | Start of string | `^hello` matches "hello world" |
| `$` | End of string | `world$` matches "hello world" |
| `[abc]` | Any of a, b, c | `[aeiou]` matches vowels |
| `[^abc]` | Not a, b, or c | `[^0-9]` matches non-digits |
import re
text = "My email is john@example.com"
# search() - find first match
match = re.search(r"\w+@\w+\.\w+", text)
if match:
print(match.group()) # john@example.com
# findall() - find all matches
text = "Call me: 555-1234 or 555-5678"
numbers = re.findall(r"\d{3}-\d{4}", text)
print(numbers) # ['555-1234', '555-5678']
# match() - check if string starts with pattern
if re.match(r"My", text):
print("Text starts with 'My'")
# sub() - replace patterns
result = re.sub(r"\d", "X", "Call 555-1234")
print(result) # Call XXX-XXXXimport re
def is_valid_email(email):
"""Check if email matches basic pattern"""
pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
return re.match(pattern, email) is not None
print(is_valid_email("john@example.com")) # True
print(is_valid_email("invalid.email")) # False
print(is_valid_email("user+tag@site.org")) # Trueimport re
text = "Date: 2026-02-20"
# Parentheses create groups
match = re.search(r"(\d{4})-(\d{2})-(\d{2})", text)
if match:
year, month, day = match.groups()
print(f"Year: {year}, Month: {month}, Day: {day}")
# Year: 2026, Month: 02, Day: 20| Concept | Remember |
|---|---|
| Import re module | Always start with `import re` |
| Use raw strings | Use `r""` for patterns to avoid escape issues |
| search() vs match() | `search()` finds anywhere; `match()` checks start |
| Metacharacters matter | `.` `\d` `\w` `\s` `^` `$` are essential |
| Quantifiers control | `*` `+` `?` `{n,m}` specify repetition |
Learn to split and join text using string operations.
Ready to practice? Challenges | Quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep • Build Real • Verify Skills • Launch Forward