How to write a regular expression to match either a or b in Python?

In Python regular expressions, one of the common tasks is matching either one character or another. This can be done easily by using the re module along with the pipe symbol |, which acts as an OR operator.

In this article, we'll explore how to match either 'a' or 'b' in a string using regex with different methods.

Using re.search() with the | Symbol

The re.search() method combined with the | (OR) symbol allows us to search for multiple patterns within a single string. It returns a match object if any of the patterns are found.

The | symbol acts as an "or" operator in the regular expression. It tells the re.search() method to look for any patterns separated by the | within the input string. The function stops at the first match it finds ?

Example

To check if a string contains either 'a' or 'b', we can write the regular expression as 'a|b', which means match either 'a' or 'b' ?

import re

text = "cat"
pattern = r"a|b"
result = re.search(pattern, text)

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

The output of the above code is ?

Match found: a

Using re.match() for Starting Check

The re.match() function checks if the beginning of a string matches a pattern. If a pattern is found, it returns a match object; otherwise, it returns None.

To check whether a string starts with 'a' or 'b', we can use re.match(r"a|b", text). This expression checks if the string starts with either the character 'a' or 'b' ?

Example

The following example demonstrates the basic usage of re.match() method to check for a match at the beginning of the string that starts with 'a' or 'b' ?

import re

text = "apple"
pattern = r"a|b"
result = re.match(pattern, text)

if result:
    print("Starts with a or b:", result.group())
else:
    print("Does not start with a or b.")

The output of the above code is ?

Starts with a or b: a

Using re.findall() for Multiple Matches

The re.findall() method finds all the parts of a string that match a specific pattern. If we want to find all instances of 'a' or 'b' in a string, this method will return a list containing each 'a' and each 'b' it finds.

Example

Let's extract all instances of 'a' or 'b' from a string ?

import re

text = "banana bread"
pattern = r"a|b"

matches = re.findall(pattern, text)
print("Matches found:", matches)

The output of the above code is ?

Matches found: ['b', 'a', 'a', 'a', 'b', 'a']

Using Character Class [ab]

An alternative to the pipe symbol is using character classes. The pattern [ab] matches either 'a' or 'b' and is more concise for single characters ?

import re

text = "zebra"
pattern = r"[ab]"
matches = re.findall(pattern, text)

print("Matches found:", matches)

The output of the above code is ?

Matches found: ['b', 'a']

Comparison

Method Pattern Purpose Returns
re.search() a|b Find first match anywhere Match object or None
re.match() a|b Match at string start Match object or None
re.findall() a|b Find all matches List of matches
Character class [ab] Match single characters Depends on function used

Conclusion

Use the pipe symbol | or character class [ab] to match either 'a' or 'b' in Python regex. Choose re.search() for first match, re.match() for start matching, and re.findall() for all matches.

Updated on: 2026-03-24T19:18:52+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements