10 Python Regular Expression Examples and How to Use Them
This article introduces Python's re module and demonstrates ten practical regular‑expression examples—including basic matching, email extraction, finding multiple patterns, substitution, anchoring, wildcard matching, grouping, repetition, non‑greedy matching, and look‑behind—complete with code snippets and expected outputs.
Regular expressions are a powerful text‑matching tool; Python's re module provides support for searching, replacing, and extracting data.
Example 1: Basic matching
import re
text = "Hello World!"
pattern = r"World"
match = re.search(pattern, text)
if match:
print(f"Found: {match.group()}")
else:
print("Not found")Output:
Found: WorldExample 2: Matching an email address
text = "Please contact me at [email protected]."
pattern = r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}"
match = re.search(pattern, text)
if match:
print(f"Email found: {match.group()}")
else:
print("No email found")Output:
Email found: [email protected]Example 3: Extracting multiple matches
text = "My numbers are 1234 and 5678."
pattern = r"\d+"
matches = re.findall(pattern, text)
print("Numbers found:", matches)Output:
Numbers found: ['1234', '5678']Example 4: Replacing text
text = "The price is $100."
pattern = r"\$\d+"
replacement = "$99"
new_text = re.sub(pattern, replacement, text)
print(new_text)Output:
The price is $99.Example 5: Matching start and end of a line
text = "Start of line"
pattern = r"^Start"
match = re.match(pattern, text)
if match:
print("Matched start of the line")
else:
print("No match")
text = "End of line"
pattern = r"line$"
match = re.search(pattern, text)
if match:
print("Matched end of the line")
else:
print("No match")Output:
Matched start of the line
Matched end of the lineExample 6: Matching any characters
text = "Any character here."
pattern = r".+here"
match = re.search(pattern, text)
if match:
print(f"Matched: {match.group()}")
else:
print("No match")Output:
Matched: Any character here.Example 7: Using parentheses for grouping
text = "My name is John Doe."
pattern = r"My name is (John Doe)"
match = re.search(pattern, text)
if match:
print(f"Name: {match.group(1)}")
else:
print("No match")Output:
Name: John DoeExample 8: Matching repeated characters
text = "Look for aaaa"
pattern = r"a{4}"
match = re.search(pattern, text)
if match:
print(f"Matched: {match.group()}")
else:
print("No match")Output:
Matched: aaaaExample 9: Non‑greedy matching
text = "The quick brown fox jumps over the lazy dog."
pattern = r"fox.*dog"
match = re.search(pattern, text)
if match:
print(f"Matched: {match.group()}")
else:
print("No match")
# Non‑greedy match
pattern = r"fox.*?dog"
match = re.search(pattern, text)
if match:
print(f"Non-greedy Matched: {match.group()}")
else:
print("No match")Output:
Matched: fox jumps over the lazy dog.
Non-greedy Matched: fox jumps over the lazy dog.Example 10: Positive look‑behind
text = "This is a test. This is only a test."
pattern = r"(?<=This is )a test"
matches = re.findall(pattern, text)
print("Matches:", matches)Output:
Matches: ['a test', 'only a test']The examples demonstrate the flexibility and power of regular expressions, from simple string matching to complex data extraction and replacement, making them invaluable for processing large text datasets.
Tip: Use online regex testing tools to debug and optimize patterns, and keep expressions as simple as possible for maintainability.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.