B Regular Expression in Python



In this article, we will see the concept of B-regular expressions in Python. Regular expressions (regex) are a useful tool for matching patterns in strings. In Python, the re module helps you to work with regex patterns.

What is a B Regular Expression?

A 'B' regular expression is a type of regex that searches and matches particular patterns within a string. For example, it can be used to find out whether a string starts or ends with a given letter or whether it matches the pattern's set criteria.

Use the re Module

For using regular expressions in Python, you need to import the re module. This module basically provides many features for searching and modifying strings using regex patterns.

import re

Examples of B Regular Expressions

Below are some examples to show you how a regular expression works in Python -

Example: Using re.search()

In this program, we will search for the word "world" in the sentence given "Hello, world!" with the help of re.search(). If it is found so it will print the word; otherwise, it will print "Not Found!".

Open Compiler
import re # Searching for a simple pattern in a string text = "Hello, world!" pattern = "world" # Use re.search result = re.search(pattern, text) if result: print(f"Found: {result.group()}") else: print("Not found!")

This output will be displayed when the program runs -

Found: world

Example: Using re.match()

In this example, we will check if the string starts with "Hello" using re.match(). If it does, it will print "Match found!" Otherwise, it will print "No match found!".

Open Compiler
import re # Checking if a string starts with a specific pattern text = "Hello, Tutorialspoint!" pattern = "Hello" # Use re.match result = re.match(pattern, text) if result: print("Match found!") else: print("No match found!")

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

Match found!

Example: Using re.findall()

The below program will find all the numbers in the given string with the help of re.findall(). We will use the regex pattern '\d+' to match sequences of digits.

Open Compiler
import re # Finding all digits in a string text = "My number is 888 and my friend's number is 999." # Pattern to find one or more digits pattern = r'\d+' # Use re.findall to get all digit occurrences numbers = re.findall(pattern, text) print(f"Found numbers: {numbers}")

This will generate the following result -

Found numbers: ['888', '999']

Example: Using the re.sub()

Here we are going to use the re.sub() method of Python's re module to replace all the vowels present in the string "Hello, Everyone!" with asterisks.

Open Compiler
import re # Replace all vowels with asterisks text = "Hello, Everyone!" # Pattern to find all vowels pattern = r'[aeiou]' # Use re.sub to replace vowels new_text = re.sub(pattern, '*', text) print(new_text)

This will produce the following result -

H*ll*, *v*ry*n*!
Updated on: 2025-04-23T12:38:24+05:30

642 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements