Regular expressions, also known as regex or RegEx, are an indispensable tool in Python for text processing and pattern matching. In this article, we will delve into common regex patterns and essential functions that will empower you to manipulate text effectively in Python.
Introduction to Python Regular Expressions
Regular expressions are sequences of characters used to define search patterns. They enable you to find, match, and manipulate strings based on specific criteria, making them a fundamental component of text processing in Python. To begin, import the re
module:
import re
Common Regex Patterns
1. Matching Digits
You can use \d
to match any digit (0-9). For example:
text = "The price of the product is $99.99."
pattern = r"\d+"
matches = re.findall(pattern, text)
if matches:
print("Found:", matches)
else:
print("Not found.")
Output:
Found: ['99', '99']
2. Matching Words
Matching words is simple with \w
, which matches any word character (letters, digits, or underscores):
text = "Python is an amazing language."
pattern = r"\w+"
matches = re.findall(pattern, text)
if matches:
print("Found:", matches)
else:
print("Not found.")
Output:
Found: ['Python', 'is', 'an', 'amazing', 'language']
3. Matching Email Addresses
To match email addresses, use \S+@\S+
:
text = "Contact support@example.com or info@example.org for assistance."
pattern = r"\S+@\S+"
matches = re.findall(pattern, text)
if matches:
print("Found:", matches)
else:
print("Not found.")
Output:
Found: ['support@example.com', 'info@example.org']
Essential Functions
1. re.search()
The re.search()
function searches for the first occurrence of a pattern:
text = "Python is the best language for beginners."
pattern = r"best"
match = re.search(pattern, text)
if match:
print("Found:", match.group())
else:
print("Not found.")
Output:
Found: best
2. re.sub()
The re.sub()
function replaces occurrences of a pattern with a specified string:
text = "Hello, World!"
pattern = r"World"
new_text = re.sub(pattern, "Python", text)
print(new_text)
Output:
Hello, Python!