Regular Expression in Python



Regular expressions are a useful tool for searching and manipulating strings in Python. Among the several patterns and methods available, the [d+] expression is used to match one or more digits in a string.

In this article, we will go over the [d+] regular expression pattern, the way it is used, and five code examples with step-by-step explanations to help you understand and apply it to your Python projects.

Understanding the [d+] Pattern

Before we see the code examples, let us first understand the [d+] pattern. So basically [d+] is a combination of two elements -

  • [d]: This is a shorthand character class for the digits 0 to 9.

  • [+]: This is a quantifier that indicates "one or more" of the preceding pattern.

  • Combining these two elements, [d+] creates a pattern that matches one or more consecutive digits in a string.

Example: Basic usage of [d+]

In this example, we will define a [d+] pattern and search for one or more consecutive digits in the given phone number string. If the re.search() function returns a match object, we print 'True', otherwise, we print 'False'.

Open Compiler
import re phone_number = '234567890' pattern = r'\d+' result = re.search(pattern, phone_number) if result: print('True') else: print('False')

After running the program, you will get this result -

True

Example: Matching a Specific Number

The below Python program will check if a password has three numbers in a row or not. It basically uses a regular expression to find the pattern. And if it finds the pattern, it will print True; otherwise, it prints False.

Open Compiler
import re password = 'mypassword123' pattern = r'\d{3}' result = re.search(pattern, password) if result: print('True') else: print('False')

This output will be displayed when the program runs -

True

Matching a with one or More "d"

In this Python program, we are going to check whether there are any numbers or digits present in a given string. We will use regular expressions to find one or more digits. The search() method will find the first match in the string. So if a match is found, it will print the number with the help of group() and group(0).

Open Compiler
import re # Define the regular expression pattern pattern = r'd+' # Define the string to be searched string = 'dddabc abiuwdd oiwddd' # Use the search() method here matches = re.findall(pattern, string) print(matches) # Define a capture group to capture the match pattern = r'\d+' matches = re.search(pattern, string, re.MULTILINE) if matches: # Print the match print(matches.group(0))

Output

After running the program, you will get this result -

ddd
ddd

Using a Lookahead

To match a string with one or more "d" characters and use a lookahead, we can use the following program -

Open Compiler
import re # Define the regular expression pattern pattern = r'd+(?=abc)'; # Define the string to be searched string = 'dddabc' # Use the search() method here matches = re.search(pattern, string) # Print the match if matches: print(matches.group())

You will see this result after executing the program -

ddd

Using a Capture Group

To match a string with one or more "d" characters and use a capture group, we can use the following code -

Open Compiler
import re # Define the regular expression pattern pattern = r'd+(?P<abc>abc)'; # Define the string to be searched string = 'dddabc' # Use the search() method here matches = re.search(pattern, string) # Print the match if matches: print(matches.group()) print(matches.group('abc'))

The program gives this output when it is run -

dddabc
abc
Updated on: 2025-04-23T12:22:53+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements