Regular expressions, commonly known as Regex or RegEx, are a powerful tool in the Python programming language. They allow you to perform advanced string manipulation and text pattern matching with ease. In this article, we will provide you with a thorough introduction to Python Regular Expressions, complete with examples and output to help you grasp this essential topic.
What is a Regular Expression?
A regular expression is a sequence of characters that defines a search pattern. It allows you to match and manipulate strings based on specific criteria. Python provides the re
module for working with regular expressions, making it a versatile tool for text processing tasks.
Getting Started with the re
Module
Before diving into examples, let’s import the re
module:
import re
Basic Regex Patterns
1. Matching a Word
Suppose you want to find the word “Python” in a given text. You can use the search
method:
text = "Python is an amazing language."
pattern = r"Python"
match = re.search(pattern, text)
if match:
print("Found:", match.group())
else:
print("Not found.")
Output:
Found: Python
2. Matching Digits
To match a series of digits, use \d
:
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']
Advanced Regex Patterns
1. Matching Email Addresses
Regex can help you extract email addresses from a text:
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']
2. Extracting Dates
You can extract dates from text using regex as well:
text = "The event will be held on 05/25/2024."
pattern = r"\d{2}/\d{2}/\d{4}"
matches = re.findall(pattern, text)
if matches:
print("Found:", matches)
else:
print("Not found.")
Output:
Found: ['05/25/2024']