Get True/False from Python Regular Expressions



The re module provides tools while working with Python's regular expression (regex), which allows us to search for any specific pattern within the strings.

Using the 're' module provides two functions named match() and search(), which accept a pattern (regular expression) and return True or False. The match() function returns 'None' if no pattern is detected, or it returns the match in the form of an object (in case of a match). In addition to these, we can also use the fullmatch() function to get a boolean result from a regular expression.

  • re.match(): Checks if the pattern matches the beginning of the string.

  • re.search(): This function scans the entire string to find the first occurrence of the pattern.

  • re.fullmatch(): Checks if the entire string matches the pattern exactly and returns a match object.

Using the re.match() Function

Using re.match(), we can check if a regular expression or pattern matches the beginning of the given string. It accepts both the pattern (regular expression) and the string as parameters, and if the pattern matches ( the beginning of ) the string, it returns a Match object, otherwise, it returns None.

Example

In the following program, we are verifying whether the given string starts with "hi" using the pattern "^hi" (since' ^ ' defines the position at the start of a string). In here, the bool() function evaluates to True if there's a match, otherwise, it returns False.

import re

pattern = r"^hi"
text = "hello world"

match = re.match(pattern, text)

# Evaluates to True if there's a match
is_match = bool(match)  

print(is_match)

Output

When you run the program, it will show this output -

False

To find the first occurrence of the pattern, the re.search() method scans through the entire string and returns the match, if the pattern is found anywhere in the string.

Example

In the program below, we are simply searching for a word ("world") in the given string ("hello world").

import re

pattern = r"world"
text = "hello world"

match = re.search(pattern, text)

# Evaluates to True if there's a match
is_match = bool(match)  

print(is_match)  

Output

After running the program, you will get this result -

True

Using the re.fullmatch() Function

The re.fullmatch() function in Python's re module attempts to match the entire string based on the regular expression. It returns the match (in the form of an object) if the pattern matches the entire string from beginning to end, and None otherwise.

Example

In the below, both string and pattern are 'hello world' with no additional characters. So the 're.fullmatch()' finds an exact match and returns a match object.

import re

pattern = r"hello world"
text = "hello world"

match = re.fullmatch(pattern, text)

# Evaluates to True if there's a match
is_match = bool(match) 

print(is_match) 

Output

This output will be displayed when the program runs -

True
Updated on: 2025-04-23T11:40:41+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements