03-02-2020, 11:43 AM
Python Regex Multiple Repeat Error
Just like me an hour ago, you’re probably sitting in front of your regular expression code, puzzled by a strange error message:
How does it arise? Where does it come from? And, most importantly, how can you get rid of it?
This article gives you answers to all of those questions. Alternatively, you can also watch my short explainer video that shows you real quick how to resolve this error:
How Does the Multiple Repeat Error Arise in Python Re?
Python’s regex library re throws the multiple repeat error when you try to stack two regex quantifiers on top of each other. For example, the regex will cause the multiple repeat error. You can get rid of this error by avoiding to stack quantifiers on top of each other.
Here’s an example:
I have shortened the error message to focus on the relevant parts. In the code, you first import the regex library re. You then use the function (see this blog tutorial) to find the pattern in the string .
However, this doesn’t make a lot of sense: what’s the meaning of the pattern anyway?
[Tips] What’s the Source of the Multiple Repeat Error and How to Avoid It?
The error happens if you use the Python regex package. There are many different reasons for it but all of them have the same source: you stack quantifiers on top of each other.
If you don’t know what a quantifier is, scroll down and read the following subsection where I show you exactly what it is.
Here’s a list of reasons for the error message. Maybe your reason is among them?
Oftentimes, the error appears if you don’t properly escape the special quantifier metacharacters in your regex pattern.
Here’s a StackOverflow post that shows some code where this happened:
I edited the given code snippet to show the important part. The code fails because of a. Can you see why?
The reason is that the regex contains two plus quantifiers stacked on top of each other in the substring . Get rid of those and the code will run again!
Python Regex Quantifiers
The word “quantifier” originates from latin: it’s meaning is quantus = how much / how often.
This is precisely what a regular expression quantifier means: you tell the regex engine how often you want to match a given pattern.
If you think you don’t define any quantifier, you do it implicitly: no quantifier means to match the regular expression exactly once.
So what are the regex quantifiers in Python?
Quantifier |
Meaning
|
Match regular expression zero or one times
|
Match regular expression zero or more times
|
Match regular expression one or more times
|
Match regular expression exactly m times
|
Match regular expression between m and n times (included)
Note that in this tutorial, I assume you have at least a remote idea of what regular expressions actually are. If you haven’t, no problem, check out my detailed regex tutorial on this blog.
You see in the table that the quantifiers, , , , and define how often you repeat the matching of regex .
Let’s have a look at some examples—one for each quantifier:
In each line, you try a different quantifier on the same text. And, interestingly, each line leads to a different output:
You’ve learned the basic quantifiers of Python regular expressions.
Where to Go From Here?
To summarize, you’ve learned that the multiple repeat error appears whenever you try to stack multiple quantifiers on top of each other. Avoid this and the error message will disappear.
If you want to boost your Python regex skills to the next level, check out my free in-depth regex superpower tutorial (20,000+) words. Or just bookmark the article for later read.
Just like me an hour ago, you’re probably sitting in front of your regular expression code, puzzled by a strange error message:
Code:
re.error: multiple repeat at position xHow does it arise? Where does it come from? And, most importantly, how can you get rid of it?
This article gives you answers to all of those questions. Alternatively, you can also watch my short explainer video that shows you real quick how to resolve this error:
How Does the Multiple Repeat Error Arise in Python Re?
Python’s regex library re throws the multiple repeat error when you try to stack two regex quantifiers on top of each other. For example, the regex
Code:
'a++'Here’s an example:
Code:
>>> import re
>>> re.findall('a++', 'aaaa')
Traceback (most recent call last): File "<pyshell#29>", line 1, in <module> re.findall('a++', 'aaaa') File "C:\Users\xcent\AppData\Local\Programs\Python\Python37\lib\re.py", line 223, in findall ...
re.error: multiple repeat at position 2I have shortened the error message to focus on the relevant parts. In the code, you first import the regex library re. You then use the
Code:
re.findall(pattern, string)Code:
'a++'Code:
'aaaa'However, this doesn’t make a lot of sense: what’s the meaning of the pattern
Code:
a++[Tips] What’s the Source of the Multiple Repeat Error and How to Avoid It?
The error happens if you use the Python regex package
Code:
reIf you don’t know what a quantifier is, scroll down and read the following subsection where I show you exactly what it is.
Here’s a list of reasons for the error message. Maybe your reason is among them?
- You use the regex pattern for any regex expressionCode:
'X++'. To avoid this error, get rid of one quantifier.Code:X
- You use the regex pattern for any regex expressionCode:
'X+*'. To avoid this error, get rid of one quantifier.Code:X
- You use the regex pattern for any regex expressionCode:
'X**'. To avoid this error, get rid of one quantifier.Code:X
- You use the regex pattern for any regex expressionCode:
'X{m,n}*'and number of repetitionsCode:XandCode:m. To avoid this error, get rid of one quantifier.Code:n
- You try to match a number of characters and use a second quantifier on top of it such asCode:
'+'. In this case, you should escape the first quantifier symbolCode:'+?'.Code:'\+'
- You try to match a number of characters and use a second quantifier on top of it such asCode:
'*'. Avoid this error by escaping the first quantifier symbolCode:'*+'.Code:'\*'
Oftentimes, the error appears if you don’t properly escape the special quantifier metacharacters in your regex pattern.
Here’s a StackOverflow post that shows some code where this happened:
Code:
...
term = 'lg incite" OR author:"http++www.dealitem.com" OR "for sale'
p = re.compile(term, re.IGNORECASE) ...I edited the given code snippet to show the important part. The code fails because of a
Code:
multiple repeat errorThe reason is that the regex
Code:
'lg incite" OR author:"http++www.dealitem.com" OR "for sale'Code:
'http++'Python Regex Quantifiers
The word “quantifier” originates from latin: it’s meaning is quantus = how much / how often.
This is precisely what a regular expression quantifier means: you tell the regex engine how often you want to match a given pattern.
If you think you don’t define any quantifier, you do it implicitly: no quantifier means to match the regular expression exactly once.
So what are the regex quantifiers in Python?
Quantifier |
Meaning
Code:
A?Match regular expression
Code:
ACode:
A*Match regular expression
Code:
ACode:
A+Match regular expression
Code:
ACode:
A{m}Match regular expression
Code:
ACode:
A{m,n}Match regular expression
Code:
ANote that in this tutorial, I assume you have at least a remote idea of what regular expressions actually are. If you haven’t, no problem, check out my detailed regex tutorial on this blog.
You see in the table that the quantifiers
Code:
?Code:
*Code:
+Code:
{m}Code:
{m,n}Code:
ALet’s have a look at some examples—one for each quantifier:
Code:
>>> import re
>>> re.findall('a?', 'aaaa')
['a', 'a', 'a', 'a', '']
>>> re.findall('a*', 'aaaa')
['aaaa', '']
>>> re.findall('a+', 'aaaa')
['aaaa']
>>> re.findall('a{3}', 'aaaa')
['aaa']
>>> re.findall('a{1,2}', 'aaaa')
['aa', 'aa']In each line, you try a different quantifier on the same text
Code:
'aaaa'- The zero-or-one regex matches four times oneCode:
'a?'. Note that it doesn’t match zero characters if it can avoid doing so.Code:'a'
- The zero-or-more regex matches once fourCode:
'a*'s and consumes them. At the end of the string, it can still match the empty string.Code:'a'
- The one-or-more regex matches once fourCode:
'a+'s. In contrast to the previous quantifier, it cannot match an empty string.Code:'a'
- The repeating regex matches up to threeCode:
'a{3}'s in a single run. It can do so only once.Code:'a'
- The repeating regex matches one or twoCode:
'a{1,2}'s. It tries to match as many as possible.Code:'a'
You’ve learned the basic quantifiers of Python regular expressions.
Where to Go From Here?
To summarize, you’ve learned that the multiple repeat error appears whenever you try to stack multiple quantifiers on top of each other. Avoid this and the error message will disappear.
If you want to boost your Python regex skills to the next level, check out my free in-depth regex superpower tutorial (20,000+) words. Or just bookmark the article for later read.

