Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Regex Multiple Repeat Error
1
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:

Code:
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:



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

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

However, this doesn’t make a lot of sense: what’s the meaning of the pattern
Code:
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
Code:
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
    Code:
    'X++'
    for any regex expression
    Code:
    X
    . To avoid this error, get rid of one quantifier.

  • You use the regex pattern
    Code:
    'X+*'
    for any regex expression
    Code:
    X
    . To avoid this error, get rid of one quantifier.

  • You use the regex pattern
    Code:
    'X**'
    for any regex expression
    Code:
    X
    . To avoid this error, get rid of one quantifier.

  • You use the regex pattern
    Code:
    'X{m,n}*'
    for any regex expression
    Code:
    X
    and number of repetitions
    Code:
    m
    and
    Code:
    n
    . To avoid this error, get rid of one quantifier.

  • You try to match a number of characters
    Code:
    '+'
    and use a second quantifier on top of it such as
    Code:
    '+?'
    . In this case, you should escape the first quantifier symbol
    Code:
    '\+'
    .

  • You try to match a number of characters
    Code:
    '*'
    and use a second quantifier on top of it such as
    Code:
    '*+'
    . Avoid this error by escaping the first quantifier symbol
    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 error
. Can you see why?

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

Quantifier |
Meaning

Code:
A?
|
Match regular expression
Code:
A
zero or one times

Code:
A*
|
Match regular expression
Code:
A
zero or more times

Code:
A+
|
Match regular expression
Code:
A
one or more times

Code:
A{m}
|
Match regular expression
Code:
A
exactly m times

Code:
A{m,n}
|
Match regular expression
Code:
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
Code:
?
,
Code:
*
,
Code:
+
,
Code:
{m}
, and
Code:
{m,n}
define how often you repeat the matching of regex
Code:
A
.

Let’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'
. And, interestingly, each line leads to a different output:
  • The zero-or-one regex
    Code:
    'a?'
    matches four times one
    Code:
    'a'
    . Note that it doesn’t match zero characters if it can avoid doing so.

  • The zero-or-more regex
    Code:
    'a*'
    matches once four
    Code:
    'a'
    s and consumes them. At the end of the string, it can still match the empty string.

  • The one-or-more regex
    Code:
    'a+'
    matches once four
    Code:
    'a'
    s. In contrast to the previous quantifier, it cannot match an empty string.

  • The repeating regex
    Code:
    'a{3}'
    matches up to three
    Code:
    'a'
    s in a single run. It can do so only once.

  • The repeating regex
    Code:
    'a{1,2}'
    matches one or two
    Code:
    '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.
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: