MCA
REGULAR EXPRESSIONS // MASTER THE CONCEPTS // WRITE BRUTAL CODE // REGULAR EXPRESSIONS // MASTER THE CONCEPTS // WRITE BRUTAL CODE //
BACK TO SYLLABUS
MEDIUM

REGULAR EXPRESSIONS

Pattern matching and text processing

CONCEPTS

01The re module
02Searching patterns (re.search)
03Finding all matches (re.findall)
04Replacing strings (re.sub)
05Metacharacters and character classes
06Groups and capturing

SYNTAX_DEMO

Parsing and matching strings
import re

text = "Contact us at support@example.com or sales@test.com"
# Extract email addresses
pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}"
emails = re.findall(pattern, text)

print("Found emails:", emails)

# Substitute text
censored = re.sub(pattern, "[CENSORED]", text)
print(censored)