Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How does [d+] regular expression work 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, how it works, and provide code examples with explanations to help you understand and apply it to your Python projects.
Understanding the \d+ Pattern
The \d+ pattern consists of two elements ?
-
\d: This is a shorthand character class that matches any digit from 0 to 9.
-
+: This is a quantifier that means "one or more" of the preceding pattern.
-
Combined, \d+ matches one or more consecutive digits in a string.
Basic Usage of \d+
Here's how to use \d+ to find digits in a phone number ?
import re
phone_number = '234567890'
pattern = r'\d+'
result = re.search(pattern, phone_number)
if result:
print('Match found:', result.group())
print('True')
else:
print('False')
Match found: 234567890 True
Finding All Digit Sequences
Use findall() to extract all digit sequences from a string ?
import re
text = 'Order 123 costs $45.99 and item 678 costs $12.50'
pattern = r'\d+'
matches = re.findall(pattern, text)
print('All digit sequences:', matches)
All digit sequences: ['123', '45', '99', '678', '12', '50']
Matching Specific Number of Digits
To match exactly 3 consecutive digits, use \d{3} ?
import re
password = 'mypassword123'
pattern = r'\d{3}'
result = re.search(pattern, password)
if result:
print('Found 3 digits:', result.group())
print('True')
else:
print('False')
Found 3 digits: 123 True
Using Lookahead with \d+
A positive lookahead matches digits only if followed by specific text ?
import re
text = '123abc and 456def'
pattern = r'\d+(?=abc)'
match = re.search(pattern, text)
if match:
print('Digits before "abc":', match.group())
else:
print('No match found')
Digits before "abc": 123
Comparison of Digit Matching Patterns
| Pattern | Matches | Example |
|---|---|---|
\d |
Single digit | 5 in "a5b" |
\d+ |
One or more digits | 123 in "abc123def" |
\d{3} |
Exactly 3 digits | 456 in "abc456789" |
\d* |
Zero or more digits | "" or "123" |
Practical Example: Extracting Numbers from Text
Extract and sum all numbers from a shopping receipt ?
import re
receipt = "Item1: $25, Item2: $15, Tax: $4, Total: $44"
pattern = r'\d+'
numbers = re.findall(pattern, receipt)
print('Found numbers:', numbers)
# Convert to integers and calculate sum
total = sum(int(num) for num in numbers)
print('Sum of all numbers:', total)
Found numbers: ['1', '25', '2', '15', '4', '44'] Sum of all numbers: 101
Conclusion
The \d+ pattern is essential for matching one or more consecutive digits in strings. Use re.search() for the first match, re.findall() for all matches, and combine with quantifiers like {3} for specific requirements.
