Last modified: Nov 08, 2024 By Alexander Williams

Python re.match: Pattern Matching at String Beginning

The re.match() function in Python is a powerful tool for pattern matching that specifically looks for matches at the beginning of strings, making it ideal for validating text formats and extracting data.

Understanding re.match Basics

Unlike its cousin re.search(), re.match() only matches patterns at the start of the string. This makes it particularly useful when working with structured text like phone numbers or formatted data.

If you're interested in processing phone numbers specifically, check out our guide on Working with Phone Numbers in Python.

Basic Syntax and Usage


import re

pattern = r"Hello"
text = "Hello, World!"
result = re.match(pattern, text)

if result:
    print("Match found:", result.group())
else:
    print("No match found")


Match found: Hello

Match Object Methods

When re.match() finds a match, it returns a Match object with several useful methods:


import re

text = "Python 3.9"
pattern = r"(\w+)\s(\d\.\d)"
match = re.match(pattern, text)

print("Group():", match.group())     # Full match
print("Group(1):", match.group(1))   # First group
print("Group(2):", match.group(2))   # Second group
print("Start:", match.start())       # Start position
print("End:", match.end())           # End position


Group(): Python 3.9
Group(1): Python
Group(2): 3.9
Start: 0
End: 9

Common Use Cases

One common application is validating phone numbers. Here's a simple example that builds on our phone number extraction guide:


import re

def validate_phone(number):
    pattern = r"^\+?1?\d{10}$"
    return bool(re.match(pattern, number))

# Test cases
print(validate_phone("1234567890"))      # Basic format
print(validate_phone("+11234567890"))    # International format
print(validate_phone("123-456-7890"))    # Hyphenated format


True
True
False

Important Considerations

Remember that re.match() only matches at the beginning of the string. For matching anywhere in the string, use re.search() instead.

For more complex phone number processing, you might want to explore how to get country information from phone numbers.

Error Handling


import re

def safe_match(pattern, text):
    try:
        result = re.match(pattern, text)
        return result if result else "No match found"
    except re.error as e:
        return f"Pattern error: {e}"

# Test with invalid pattern
print(safe_match("[", "test"))

Conclusion

re.match() is a valuable tool for pattern matching at string beginnings. While it has limitations, understanding its proper use cases makes it indispensable for text validation and parsing.

For more advanced text processing tasks, especially with phone numbers, check out our guide on extracting phone numbers from text.