Regular expressions, or regex, are a powerful tool in Python for searching, matching, and manipulating strings. From data validation to advanced text processing, regex provides unmatched flexibility for working with textual data. In this blog, we’ll explore the basics of regular expressions, their syntax, and practical examples to make your learning easier.
What are Regular Expressions?
Regular expressions are patterns used to match character combinations in strings. In Python, regex functionality is provided by the built-in re
module. Regex can be used for:
- Validating user input (e.g., email addresses, phone numbers).
- Extracting specific parts of a string.
- Replacing or splitting text based on patterns.
Getting Started with Python’s re
Module
To use regular expressions, first, import the re
module:
import re
Basic Syntax and Patterns
Here are some commonly used regex patterns:
Pattern | Description | Example |
---|---|---|
. | Matches any character except newline. | a.c matches abc , a1c . |
^ | Matches the start of a string. | ^hello matches hello world . |
$ | Matches the end of a string. | world$ matches hello world . |
* | Matches 0 or more repetitions. | ab* matches a , ab , abb . |
+ | Matches 1 or more repetitions. | ab+ matches ab , abb . |
? | Matches 0 or 1 repetition. | ab? matches a , ab . |
[] | Matches any one of the characters inside. | [abc] matches a , b , c . |
{m,n} | Matches between m and n repetitions. | a{2,4} matches aa , aaa . |
\d | Matches any digit (0–9). | \d+ matches 123 . |
\w | Matches any word character (a-z, A-Z, 0-9, _). | \w+ matches hello123 . |
\s | Matches any whitespace character. | \s+ matches spaces. |
Common Regex Functions in Python
re.match()
- Checks for a match at the beginning of a string.
- Example:
import re
result = re.match(r'hello', 'hello world')
if result: print("Match found!") # Output: Match found!
re.search()
- Searches the entire string for a match.
- Example:
result = re.search(r'world', 'hello world')
if result: print("Match found!") # Output: Match found!
re.findall()
- Returns a list of all matches in the string.
- Example:
-
result = re.findall(r'\d+', 'Order 123, item 456')
print(result) # Output: ['123', '456']
re.sub()
- Replaces matches with a specified string.
- Example:
result = re.sub(r'\d+', 'X', 'Order 123, item 456')
print(result) # Output: Order X, item X
re.split()
- Splits the string at each match.
- Example:
result = re.split(r'\s+', 'Split this sentence')
print(result) # Output: ['Split', 'this', 'sentence']
Practical Examples of Regex
1. Validating an Email Address
email = "[email protected]"
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
if re.match(pattern, email):
print("Valid email address")
else:
print("Invalid email address")
2. Extracting Phone Numbers
text = "Contact us at 123-456-7890 or 987-654-3210."
pattern = r'\d{3}-\d{3}-\d{4}'
phone_numbers = re.findall(pattern, text)
print(phone_numbers) # Output: ['123-456-7890', '987-654-3210']
3. Removing Special Characters
text = "Hello, World! @2024"
pattern = r'[^a-zA-Z0-9\s]'
cleaned_text = re.sub(pattern, '', text)
print(cleaned_text) # Output: Hello World 2024
4. Password Validation
password = "SecurePass123!"
pattern = r'^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$'
if re.match(pattern, password):
print("Strong password")
else:
print("Weak password")
Tips for Writing Regex
- Use Online Tools:
- Tools like regex101.com can help you test and debug your regex patterns.
- Keep It Simple:
- Write small patterns and combine them incrementally.
- Use Raw Strings (
r''
):- Always use raw strings in Python to avoid escaping special characters unnecessarily.
- Add Comments:
- Use comments in complex regex patterns for better readability.
Conclusion
Regular expressions are a must-have skill for Python developers, enabling efficient text processing and validation. While they can seem daunting at first, practice and familiarity with the syntax will make regex a powerful addition to your toolkit. Start small, use tools to experiment, and soon you’ll be crafting complex patterns with ease!
What’s your favorite regex use case? Let us know in the comments below!