Description: A regular expression is a sequence of characters that defines a search pattern.
It is used to match, search, extract, or manipulate text based on specific patterns.
Python provides the re module to work with regular expressions, enabling you to perform complex text-processing tasks efficiently.
Step-by-Step Process
Import the re Module:
Start by importing the re module to access regular expression functionalities.
Define a Pattern:
Create a regular expression pattern using a string. Patterns may include special characters
like . (any character), * (zero or more repetitions), ^ (start of string), $ (end of string), etc.
Compile the Pattern (Optional):
Use re.compile() to compile a regex pattern into a regex object for reuse, improving performance
in multiple matches.
Search/Match Using Functions:
Use functions like re.match(), re.search(), re.findall(), or re.sub() to perform operations
such as finding matches, searching for patterns, extracting groups, or replacing substrings.
Process Results:
The functions return results as strings, lists, or match objects, which can be further processed
or analyzed.
Sample Code
import re
text = "Contact us at support@example.com or sales@example.org. Call +1-800-555- 1234."
email_pattern = r'[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}'
emails = re.findall(email_pattern, text)
print("Found email addresses:", emails)
print()
phone_pattern = r'\+?\d{1,2}-\d{3}-\d{3}-\d{4}'
phone = re.search(phone_pattern, text)
if phone:
print("Found phone number:", phone.group())
print()
redacted_text = re.sub(email_pattern, '[REDACTED]', text)
print("Redacted text:", redacted_text)
print()
if re.match(r'^Contact', text):
print("The text starts with 'Contact'.")
print()
words = re.split(r'\s+', text)
print("Words in text:", words)