Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Match patterns and strings using the RegEx module in Python
The RegEx module in Python provides powerful pattern matching capabilities for text processing. Regular expressions help you search, match, and manipulate strings based on specific patterns, making them essential for data validation, text parsing, and string operations.
Getting Started with RegEx
The regular expression module comes built into Python, so no separate installation is required. To use it, simply import the module ?
import re
Essential RegEx Functions
The RegEx module provides several key functions for different pattern matching needs ?
re.compile(pattern, flags) # Compiles pattern for reuse re.search(pattern, string, flags) # Searches anywhere in string re.match(pattern, string, flags) # Matches from start of string re.split(pattern, string, max) # Splits string by pattern re.findall(pattern, string) # Returns all matches as list re.sub(pattern, repl, string) # Replaces matches with replacement re.subn(pattern, repl, string) # Like sub() but returns tuple with count
Using re.match() for Pattern Matching
The match() function checks if a pattern matches from the beginning of a string ?
import re
# Using re.compile for reusable patterns
pattern = re.compile("Hello world")
result = pattern.match("Hello world! How are things going?")
if result:
print("Pattern matches")
else:
print("Pattern does not match")
Pattern matches
You can also use match() directly without compiling ?
import re
result = re.match("Hello world", "Hello world! How are things going?")
if result:
print("Pattern matches")
else:
print("Pattern does not match")
Pattern matches
Using re.split() for String Splitting
Split strings based on patterns using split() ?
import re
# Split by non-word characters
result1 = re.split(r"\W+", "Hello,World")
print("Without capturing:", result1)
# Split with capturing groups (includes delimiter)
result2 = re.split(r"(\W+)", "Hello,World")
print("With capturing:", result2)
Without capturing: ['Hello', 'World'] With capturing: ['Hello', ',', 'World']
Using re.sub() and re.subn() for Replacement
Replace matched patterns with new text using substitution functions ?
import re
text = "Hello there. Python is fun. Hello there"
# Simple substitution
result1 = re.sub(r"there", "World", text)
print("re.sub():", result1)
# Substitution with count
result2 = re.subn(r"there", "World", text)
print("re.subn():", result2)
re.sub(): Hello World. Python is fun. Hello World
re.subn(): ('Hello World. Python is fun. Hello World', 2)
Practical Example: Password Validation
A common real-world use case is validating user input like passwords ?
import re
def validate_password(password):
# Check for at least one digit
has_digit = re.search(r"[0-9]", password)
# Check minimum length
min_length = len(password) >= 7
if has_digit and min_length:
return f"{password} is a valid password"
else:
return f"{password} is invalid. Password must be 7+ characters with at least 1 number"
# Test passwords
test_passwords = ["abc123def", "short", "longbutnodigits"]
for pwd in test_passwords:
print(validate_password(pwd))
abc123def is a valid password short is invalid. Password must be 7+ characters with at least 1 number longbutnodigits is invalid. Password must be 7+ characters with at least 1 number
Comparison of Key Functions
| Function | Purpose | Returns |
|---|---|---|
match() |
Match from string start | Match object or None |
search() |
Find anywhere in string | First match object or None |
findall() |
Find all matches | List of strings |
sub() |
Replace matches | Modified string |
Conclusion
Python's RegEx module provides powerful pattern matching capabilities for text processing tasks. Master functions like match(), search(), split(), and sub() to handle complex string operations efficiently in your Python projects.
