GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python re.match - Mastering Regular Expression Matching Python re.match() Functionlast modified April 20, 2025 Introduction to re.match
The
Unlike This function is useful for validating input formats or extracting data from structured text where patterns appear at the beginning. Basic Syntax
The syntax for re.match(pattern, string, flags=0)
The Basic Pattern MatchingLet's start with a simple example of matching a pattern at string start. basic_match.py
#!/usr/bin/python
import re
text = "hello world"
result = re.match(r'hello', text)
if result:
print("Pattern found at start")
else:
print("Pattern not found at start")
This example checks if 'hello' appears at the beginning of the string.
The raw string ( result = re.match(r'hello', text) This attempts to match the literal 'hello' at the start of the string. If successful, it returns a match object; otherwise, it returns None. if result: We check if the match was successful. The match object evaluates to True if a match was found, False otherwise. Matching with GroupsParentheses create capturing groups that extract parts of the match. groups_match.py
#!/usr/bin/python
import re
text = "2023-12-25 log entry"
result = re.match(r'(\d{4})-(\d{2})-(\d{2})', text)
if result:
print(f"Full match: {result.group(0)}")
print(f"Year: {result.group(1)}")
print(f"Month: {result.group(2)}")
print(f"Day: {result.group(3)}")
This extracts date components from a log entry format. Groups are accessed
using the Using Flags
Flags modify matching behavior. Here's how to use them with flags_match.py
#!/usr/bin/python
import re
text = "Python is awesome"
result = re.match(r'python', text, re.IGNORECASE)
if result:
print("Found match (case insensitive)")
else:
print("No match found")
The Matching Special Patterns
email_match.py
#!/usr/bin/python import re email = "[email protected]" pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' result = re.match(pattern, email) if result: print("Valid email format") else: print("Invalid email format") This checks if a string starts with a valid email format. The pattern validates the basic structure of email addresses. Matching vs. Searching
Demonstrating the difference between match_vs_search.py
#!/usr/bin/python
import re
text = "The answer is 42"
match_result = re.match(r'\d+', text)
search_result = re.search(r'\d+', text)
print(f"match result: {'found' if match_result else 'not found'}")
print(f"search result: {'found' if search_result else 'not found'}")
Named GroupsNamed groups make patterns more readable and maintainable. named_groups.py
#!/usr/bin/python
import re
text = "Temperature: 23.5C"
pattern = r'Temperature: (?P<value>\d+\.\d+)(?P<unit>[CF])'
result = re.match(pattern, text)
if result:
print(f"Value: {result.group('value')}")
print(f"Unit: {result.group('unit')}")
Named groups ( Compiled Patterns with MatchFor better performance with repeated matches, use compiled patterns. compiled_match.py
#!/usr/bin/python
import re
pattern = re.compile(r'^[A-Z][a-z]+$')
names = ["Alice", "bob", "Charlie", "david"]
for name in names:
if pattern.match(name):
print(f"{name} is properly capitalized")
else:
print(f"{name} is not properly capitalized")
Compiling the pattern once improves performance when matching multiple
strings. The Best Practices
When using
Performance Considerations
Python internally caches recently used patterns, but explicit compilation helps when the same pattern is used many times in a loop. SourcePython re.match() documentation
This tutorial covered the essential aspects of Python's AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |