re.match() in Python (original) (raw)

Last Updated : 23 Jul, 2025

re.match method in Python is used to check if a given pattern matches the beginning of a string. It’s like searching for a word or pattern at the start of a sentence. For example, we can use re.match to check if a string starts with a certain word, number, or symbol. We can do pattern matching using re.match in different ways like checking if the string starts with certain characters or more complex patterns.

Python `

import re

Checking if the string starts with "Hello"

s = "Hello, World!" match = re.match("Hello", s)

if match: print("Pattern found!") else: print("Pattern not found.")

`

Table of Content

Syntax of re.match

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

Parameters

Returns

Using re.match with Regular Expressions

Regular expressions (regex) allow us to search for more complex patterns. For example, let’s check if a string starts with a number.

Python `

import re

Checking if the string starts with a number

s = "123abc"

\d means any digit

match = re.match(r"\d", s)
if match: print("Starts with a number.") else: print("Doesn't start with a number.")

`

Output

Starts with a number.

Accessing Match Object

The result of re.match is a match object if a match is found, or None if no match is found. If a match occurs, we can get more information about it.

Python `

import re

s = "Python is great" match = re.match(r"Python", s)

if match: print(f"Match found: {match.group()}") # .group() returns the matched string else: print("No match.")

`

Output

Match found: Python