Mastering Regular Expressions (Regex) in Python: A Complete Guide with Cheat Sheet & Examples
Rohit Ramteke
Senior Technical Lead @Birlasoft | DevOps Expert | CRM Solutions | Siebel Administrator | IT Infrastructure Optimization |Project Management
Regular Expressions, also known as regex, are a powerful tool in Python for working with strings. They allow you to search, match, and manipulate text with patterns — making them essential for tasks like data validation, parsing, and text processing.
In this comprehensive guide, we will cover everything you need to know about Regular Expressions in Python, from basics to advanced concepts, along with a complete cheat sheet and real-world examples. Let's dive in!
What is a Regular Expression?
A regular expression (regex) is a sequence of characters that defines a search pattern. You can use regex to:
How to Use Regex in Python?
Python has a built-in module called re that provides support for regular expressions.
Importing the re Module
import re
Basic Regex Functions in Python
·???????? re.match(pattern, string) : Matches pattern only at the beginning of the string.
·???????? re.search(pattern, string) : Searches the string for a match (anywhere).
·???????? re.findall(pattern, string) : Returns all non-overlapping matches in a list.
·???????? re.finditer(pattern, string) : Returns an iterator yielding match objects.
·???????? re.sub(pattern, repl, string) : Substitutes occurrences of the pattern.
·???????? re.split(pattern, string) : Splits string by occurrences of pattern.
·???????? re.compile(pattern) : Compiles pattern into a regex object for reuse.
Complete Regular Expression Cheat Sheet
Sample Programs for Regular Expressions
1. Basic Match and Search
import re
text = "Hello World"
# Match
result = re.match(r"Hello", text)
print("Match:", result.group() if result else "No match")
# Search
result = re.search(r"World", text)
print("Search:", result.group() if result else "No match")
2. Find All Matches
text = "123 abc 456 def 789"
# Find all digits
result = re.findall(r'\d+', text)
print("Digits found:", result)
# Find all words
result = re.findall(r'\w+', text)
print("Words found:", result)
3. Substitution
text = "Today is 16-03-2025"
# Replace date format
new_text = re.sub(r'(\d{2})-(\d{2})-(\d{4})', r'\3/\2/\1', text)
print("Formatted Date:", new_text)
4. Splitting Strings
text = "apple, banana; cherry orange"
# Split on comma, semicolon, or space
result = re.split(r'[;, ]+', text)
print("Split List:", result)
5. Validating Email Address
email = "[email protected]"
# Email pattern
pattern = r'^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
if re.match(pattern, email):
print("Valid Email")
else:
print("Invalid Email")
6. Extracting Phone Numbers
text = "Contact us at +1-800-555-1234 or 800-555-9876"
# Phone number pattern
pattern = r'\+?\d{1,2}-\d{3}-\d{3}-\d{4}'
numbers = re.findall(pattern, text)
print("Phone Numbers:", numbers)
7. Compiling Patterns for Reuse
pattern = re.compile(r'\d+')
texts = ["There are 123 apples", "456 oranges", "No fruits here"]
for t in texts:
print(pattern.findall(t))
Real World Examples
? Validate IP Address
ip = "192.168.1.1"
pattern = r'^(\d{1,3}\.){3}\d{1,3}$'
print("Valid IP" if re.match(pattern, ip) else "Invalid IP")
? Extract HTML Tags
html = "<div>Hello</div><p>World</p>"
tags = re.findall(r'<.*?>', html)
print("Tags:", tags)
?? Tips and Tricks for Regex in Python
?? Conclusion
Regular expressions in Python can be intimidating at first, but with practice, they become a powerful ally in string processing, data cleaning, and text manipulation. This guide, with its cheat sheet and examples, should give you a solid foundation to start using regex effectively in your Python projects.
Pro Tip: Start small. Build and test regex patterns incrementally to understand how they work!
Very informative