Introduction
In this lesson on working with Regular Expressions (Regex) in Python, you'll explore how these powerful tools enable sophisticated text processing and pattern matching. Regular expressions help detect specific patterns in strings, making them invaluable in fields like data cleaning, validation, and parsing.
Understanding Regular Expressions
Regular expressions are sequences of characters that form a search pattern, commonly used for string pattern matching and manipulation. They can identify distinct text patterns such as phone numbers, email addresses, dates, and more. Regex is especially useful when dealing with complex or large text datasets.
The Python re Module
Python's re library provides functionalities to handle regular expressions. Here’s how you can utilize its core functions:
Compiling Patterns
To optimize the reuse of regular expression patterns, you can first compile them using re.compile():
import re
pattern = re.compile(r'\d{3}-\d{2}-\d{4}') # Matches a pattern like 123-45-6789
Searching Text
You can search for a pattern in a string using the search() method:
txt = "My phone number is 123-45-6789"
result = pattern.search(txt)
if result:
print('Match found:', result.group())
# Output
Match found: 123-45-6789
Matching from Start
Use match() to check if a string matches a pattern from the beginning:
result = pattern.match("123-45-6789 is my number")
if result:
print('Pattern matches at the start:', result.group())
# Output
Pattern matches at the start: 123-45-6789
Finding All Occurrences
To find all non-overlapping occurrences of a pattern, use findall():
results = pattern.findall("Here are some numbers: 123-45-6789, 987-65-4321")
print('Found patterns:', results)
# Output
Found patterns: ['123-45-6789', '987-65-4321']
Crafting Regex Patterns
Using Literals and Metacharacters
- Literals: Direct match characters like
abc. - Metacharacters: Characters like
^,$,.with special meaning, e.g.,^startmatches the start of a string.
Anchors
Anchors specify positions within a string:
^matches the start and$the end.
Character Classes
Define sets of characters to match using brackets []:
pattern = re.compile(r'[aeiou]')
Grouping, Alternation, and Quantifiers
Grouping
Use parentheses () to group parts of the pattern:
pattern = re.compile(r'(\d+)-(\d+)')
Alternation
Use the pipe | for a logical OR:
pattern = re.compile(r'cat|dog')
Quantifiers
Quantifiers control the number of times a character or group should repeat:
*: 0 or more+: 1 or more?: 0 or 1{n, m}: Between n and m times
Substitution and Splitting
Substitution
Replace parts of the text with sub():
new_text = re.sub(r'\d', '0', "There are 3 apples and 5 bananas")
print(new_text)
# Output
There are 0 apples and 0 bananas
Splitting Text
Use split() to segment text:
segments = re.split(r'[,.]', "Apples, oranges. Bananas")
print('Segments:', segments)
# Output
Segments: ['Apples', ' oranges', ' Bananas']
Summary / Key Takeaways
In this lesson on Working with Regular Expressions (Intro), you have learned:
- Regular expressions are key for pattern matching in strings, useful for data validation and parsing.
- Python's
remodule is essential for compiling, searching, matching, and finding patterns efficiently. - Basic regex patterns include literals, metacharacters, anchors, and character classes.
- Advanced patterns involve grouping, alternation, and the use of quantifiers.
- Regex substitution and splitting are practical for data cleaning and transformation tasks.
