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 to specify repetitions Regex in Python?
A regular expression is a series of characters that allows you to match strings using search patterns. In Python, the re module is used to work with regular expressions.
Repetition quantifiers in regex specify how many times a character or pattern should appear. Python regex supports several repetition operators that make pattern matching flexible and powerful.
Types of Repetition Quantifiers
Regex provides several special characters to specify repetitions ?
-
Asterisk (*): Matches zero or more occurrences of the preceding element.
-
Plus (+): Matches one or more occurrences of the preceding element.
-
Question Mark (?): Matches zero or one occurrence of the preceding element.
-
Curly Braces ({}): Specifies exact number of repetitions. For example,
{3}means exactly 3 occurrences,{2,4}means between 2 and 4 occurrences.
Using Asterisk (*)
The asterisk (*) matches zero or more occurrences of the preceding character or pattern ?
import re pattern = r'a*' # Matches 'a' zero or more times text = "aaabbaaa" matches = re.findall(pattern, text) print(matches)
['aaa', '', '', 'aaa', '']
Using Plus (+)
The plus (+) ensures the preceding element appears at least once ?
import re pattern = r'a+' # Matches 'a' one or more times text = "aaabbaaa" matches = re.findall(pattern, text) print(matches)
['aaa', 'aaa']
Using Question Mark (?)
The question mark (?) matches the preceding element zero or one time ?
import re pattern = r'a?' # Matches 'a' zero or one time text = "aaabbaaa" matches = re.findall(pattern, text) print(matches)
['a', 'a', 'a', '', '', 'a', 'a', 'a', '']
Using Curly Braces ({})
Curly braces allow precise specification of repetition counts ?
Exact Repetitions with search()
Use {n} to match exactly n occurrences ?
import re
string = 'TTTTTTPPPPPPPPPPPP'
match = re.search(r'T{4}P{3}', string)
if match:
print(match.group(0))
TTTTPPP
Range Repetitions with findall()
Use {min,max} to specify a range of repetitions ?
import re
# Match 3-5 A's followed by 2-4 B's
text = 'AAAABBBAAAAABBBBCCCCC'
matches = re.findall(r'A{3,5}B{2,4}', text)
print(matches)
['AAAABB', 'AAAAABBBB']
Practical Examples
Here's a comprehensive example showing different repetition patterns ?
import re
text = "abc a ab abb abbb phone: 123-456-7890"
# Different repetition patterns
patterns = {
r'ab*': 'a followed by zero or more b',
r'ab+': 'a followed by one or more b',
r'ab?': 'a followed by zero or one b',
r'ab{2}': 'a followed by exactly 2 b',
r'\d{3}-\d{3}-\d{4}': 'phone number pattern'
}
for pattern, description in patterns.items():
matches = re.findall(pattern, text)
print(f"{description}: {matches}")
a followed by zero or more b: ['a', 'a', 'ab', 'abb', 'abbb'] a followed by one or more b: ['ab', 'abb', 'abbb'] a followed by zero or one b: ['a', 'a', 'ab', 'ab', 'ab'] a followed by exactly 2 b: ['abb'] phone number pattern: ['123-456-7890']
Conclusion
Repetition quantifiers make regex patterns flexible and powerful. Use *, +, ? for common patterns, and {} for precise control over repetition counts. Understanding these quantifiers is essential for effective pattern matching in Python.
