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:

re.error: multiple repeat at position x

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:

Related article: Python Regex Superpower – The Ultimate Guide

Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

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 'a++' 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:

>>> 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 2

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 re.findall(pattern, string) function (see this blog tutorial) to find the pattern 'a++' in the string 'aaaa'.

However, this doesn’t make a lot of sense: what’s the meaning of the pattern a++ 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 re. 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?

  • You use the regex pattern 'X++' for any regex expression X. To avoid this error, get rid of one quantifier.
  • You use the regex pattern 'X+*' for any regex expression X. To avoid this error, get rid of one quantifier.
  • You use the regex pattern 'X**' for any regex expression X. To avoid this error, get rid of one quantifier.
  • You use the regex pattern 'X{m,n}*' for any regex expression X and number of repetitions m and n. To avoid this error, get rid of one quantifier.
  • You try to match a number of characters '+' and use a second quantifier on top of it such as '+?'. In this case, you should escape the first quantifier symbol '\+'.
  • You try to match a number of characters '*' and use a second quantifier on top of it such as '*+'. Avoid this error by escaping the first quantifier symbol '\*'.

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:

...
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 multiple repeat error. Can you see why?

The reason is that the regex 'lg incite" OR author:"http++www.dealitem.com" OR "for sale' contains two plus quantifiers stacked on top of each other in the substring 'http++'. 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?

QuantifierMeaning
A?Match regular expression A zero or one times
A*Match regular expression A zero or more times
A+Match regular expression A one or more times
A{m}Match regular expression A exactly m times
A{m,n}Match regular expression A 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 ?, *, +, {m}, and {m,n} define how often you repeat the matching of regex A.

Let’s have a look at some examples—one for each quantifier:

>>> 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 'aaaa'. And, interestingly, each line leads to a different output:

  • The zero-or-one regex 'a?' matches four times one 'a'. Note that it doesn’t match zero characters if it can avoid doing so.
  • The zero-or-more regex 'a*' matches once four 'a's and consumes them. At the end of the string, it can still match the empty string.
  • The one-or-more regex 'a+' matches once four 'a's. In contrast to the previous quantifier, it cannot match an empty string.
  • The repeating regex 'a{3}' matches up to three 'a's in a single run. It can do so only once.
  • The repeating regex 'a{1,2}' matches one or two 'a's. It tries to match as many as possible.

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.