23.Regular Expressions
Regular expressions (regex) are powerful tools used for pattern matching and text manipulation. Python provides the `re` module to work with regular expressions.
Syntax and Examples
Import the `re` module to use regular expressions in Python:
import re
1. `re.search(pattern, string)` – Searches for the pattern in the string.
2. `re.findall(pattern, string)` – Returns all non-overlapping matches of the pattern.
3. `re.sub(pattern, repl, string)` – Replaces occurrences of the pattern with `repl`.
4. `re.match(pattern, string)` – Matches the pattern only at the beginning of the string.
5. `re.compile(pattern)` – Compiles a regex pattern into a regex object.
Patterns are defined using special characters:
• `.` – Matches any character except newline
• `^` – Matches the start of the string
• `$` – Matches the end of the string
• `*` – Matches 0 or more repetitions
• `+` – Matches 1 or more repetitions
• `?` – Matches 0 or 1 repetition
• `[]` – Matches any character inside the brackets
• `|` – Acts as an OR operator
• `()` – Groups patterns
Examples
Example 1: Search for a word in a string
import re
text = “The rain in Spain”
match = re.search(“rain”, text)
if match:
print(“Match found:”, match.group())
Example 2: Find all digits in a string
import re
text = “There are 3 cats and 4 dogs”
digits = re.findall(r’\d+’, text)
print(digits) # Output: [‘3’, ‘4’]
Example 3: Replace words
import re
text = “Hello World”
new_text = re.sub(“World”, “Python”, text)
print(new_text) # Output: Hello Python
Example 4: Grouping
import re
text = “Name: John, Age: 30”
match = re.search(r”Name: (\w+), Age: (\d+)”, text)
if match:
print(“Name:”, match.group(1))
print(“Age:”, match.group(2))
Best Practices
• Use raw strings (prefix with ‘r’) for regex patterns to avoid escaping issues.
• Test your regex patterns using online tools like regex101.com.
• Use `re.compile()` for patterns used multiple times to improve performance.
• Avoid overly complex patterns; keep them readable and maintainable.