![]() |
|
[Tut] Python Regex Multiple Repeat Error - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Regex Multiple Repeat Error (/thread-93822.html) |
[Tut] Python Regex Multiple Repeat Error - xSicKxBot - 03-02-2020 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 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?
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'
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. |