ZetCode

Python re.match() Function

last modified April 20, 2025

Introduction to re.match

The re.match function checks if a regular expression matches at the beginning of a string. It's part of Python's re module.

Unlike re.search, which looks anywhere in the string, re.match only checks the start. It returns a match object if found or None otherwise.

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 is straightforward:

re.match(pattern, string, flags=0)

The pattern is the regular expression string. The string is the text to search. Optional flags modify matching behavior.

Basic Pattern Matching

Let'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 (r'') prevents Python from interpreting backslashes.

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 Groups

Parentheses 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 group method with 1-based indexing.

Using Flags

Flags modify matching behavior. Here's how to use them with re.match.

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 re.IGNORECASE flag makes the match case-insensitive. Other useful flags include re.MULTILINE and re.DOTALL.

Matching Special Patterns

re.match can validate complex patterns like email addresses.

email_match.py
#!/usr/bin/python

import re

email = "user@example.com"
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 and search.

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'}")

re.match fails because numbers aren't at the start, while re.search succeeds by finding them anywhere in the string.

Named Groups

Named 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 (?P<name>) allow accessing matches by name instead of position. This improves code clarity.

Compiled Patterns with Match

For 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 match method works like re.match.

Best Practices

When using re.match, follow these best practices:

Performance Considerations

re.match is efficient for checking string prefixes. For one-time matches, the performance difference with compiled patterns is negligible.

Python internally caches recently used patterns, but explicit compilation helps when the same pattern is used many times in a loop.

Source

Python re.match() documentation

This tutorial covered the essential aspects of Python's re.match function. Understanding pattern matching is crucial for text processing.

Author

My 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.