1. Introduction
Regular expressions—better known as regex—are like super-powered search tools. With a few clever symbols, you can sift through text, validate formats, or extract key data in seconds. Python’s built-in re module makes regex a go-to skill for tasks like cleaning messy data, finding patterns in logs, or checking user input. This cheat sheet is your quick reference to the most useful regex tricks, so you can spend less time flipping through docs and more time coding.
2. What’s Inside This Cheat Sheet
This sheet gathers everything you need to write and test patterns fast. You’ll find basic functions, common metacharacters, and character classes, plus special sequences and flags to fine-tune your matches. We’ve also included common patterns for everyday tasks, a quick example to see it all in action, and pro tips to keep your regex clean and efficient.
3. How to Use the Cheat Sheet
Keep it open while you work. Need to validate an email? Copy a ready-made pattern. Unsure about the difference between * and +? Check the metacharacter section. Test patterns in a Python shell or an online regex tester as you go. Start small, match simple text first, then build up to complex patterns by adding pieces one step at a time.
4. Quick Setup in Python
Regex in Python starts with a single import:
Use re.search() to find the first match or re.findall() to grab them all. For repeated use, compile your pattern once:
The r"" prefix tells Python to treat the string as raw, so backslashes stay put—no double escaping required.
5. Best Practices & Tips
- Go Raw: Always write patterns as raw strings: r"\d{3}".
- Name Groups: Use (?P<name>pattern) to capture with clarity.
- Test Often: Try patterns with sample text before production.
- Use Flags: Add flexibility with re.IGNORECASE or re.MULTILINE.
- Keep It Readable: Break long patterns across lines using re.VERBOSE and add comments.
What’s Inside the Sheet in Detail
Basic Functions
Key re methods at a glance—search, match, findall, sub, and split—for finding, replacing, or splitting text with ease.
Common Metacharacters
Symbols like . (any character), ^ (start of line), $ (end of line), *, +, and ? that shape your pattern’s behavior.
Character Classes
Square brackets let you target sets: [abc] for a, b, or c; [0-9] for digits; [^ ] to exclude.
Special Sequences
Shortcuts like \d for digits, \w for letters/numbers/underscore, \s for whitespace, and their uppercase opposites for “not” matches.
Useful Flags
Modifiers such as re.IGNORECASE for case-insensitive matching, re.MULTILINE for multi-line anchors, and re.DOTALL to let . match newlines.
Common Patterns
Ready-made snippets:
- Email: r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}"
- Phone: r"\+?\d[\d -]{8,12}\d"
- Date (YYYY-MM-DD): r"\d{4}-\d{2}-\d{2}"
Quick Example
This grabs every email address in the string with one line of code.
Tips
- Start simple, then refine.
- Comment complex patterns for your future self.
- Use online testers like regex101 to debug quickly.
6. Conclusion
Regex might look intimidating, but it’s really just a clever language for describing text patterns. With this Python Regex Cheat Sheet at your fingertips—covering functions, symbols, and practical patterns—you’ll match, extract, and transform text like a pro. Bookmark it, print it, and keep it handy for your next project or coding interview.