Regular Expressions (Regex) are powerful patterns for searching, validating, and manipulating text. In the Python Universe, regex is like an "advanced scanner" that finds exactly what you are looking for in any string. In this complete guide, you will master regex from basic to advanced with practical real-world examples.
Regular expressions are sequences of characters that define a search pattern. They allow you to:
- Validate formats (email, phone numbers).
- Search for patterns in texts.
- Extract specific information.
- Replace text intelligently.
import re
## Simple example: find all occurrences of 'python'
text = "Python is amazing! Learn python today. PYTHON rocks!"
matches = re.findall(r'python', text, re.IGNORECASE)
print(matches) # ['Python', 'python', 'PYTHON']
The re Module
Python uses the re module to work with regex. Key functions include:
re.search(): Finds the first occurrence.re.match(): Checks the beginning of the string.re.findall(): Finds all occurrences.re.sub(): Replaces patterns.
🔤 Basic Syntax and Metacharacters
Regex uses special characters to define patterns:
.: Any character except newline.^: Start of the string.$: End of the string.+: One or more repetitions.*: Zero or more repetitions.\d: Any digit (0-9).\w: Alphanumeric character.
Build Safer and More Maintainable Patterns
Use raw strings such as r"\d+" so Python does not process backslashes before the regex engine. Choose fullmatch() when the entire value must follow a format, search() when the pattern may appear anywhere, and finditer() when you need matches and their positions. Compile a pattern when it is reused or when flags are part of its definition.
import re
EMAIL = re.compile(r"[\w.+-]+@[\w-]+\.[\w.-]+", re.IGNORECASE)
def valid_email(value: str) -> bool:
return EMAIL.fullmatch(value.strip()) is not None
Common Mistakes and Performance
A regex is not always the best parser. Prefer json, csv, or an HTML parser for structured formats. Avoid ambiguous nested repetitions such as (a+)+ on untrusted input because they can cause excessive backtracking. Test valid, invalid, empty, and unusually long values. For complex patterns, use re.VERBOSE to add whitespace and comments, making future maintenance safer.
Conclusion
Regex is an essential tool for any developer. With practice, you will be able to create patterns to validate and extract any type of text information! For more, check our guides on Python Strings and File Manipulation. Also, consult the official Python re documentation.