- re.error: nothing to repeat at position 2 #568
- error: nothing to repeat at position 0 #66
- Python Regex Multiple Repeat Error
- How Does the Multiple Repeat Error Arise in Python Re?
- [Tips] What’s the Source of the Multiple Repeat Error and How to Avoid It?
- Python Regex Quantifiers
- Alternative Error Message (Fragments)
- Where to Go From Here?
- Regex Humor
re.error: nothing to repeat at position 2 #568
Great work with Paperless, It’s making my paperwork less time consuming.
Recently I have been getting a regex error while running the consumer. The process quits with the line «re.error: nothing to repeat at position 2» in the Traceback. It seems to relate to the systems Python file «sre_parse.py» and the line 651:
raise source.error(«nothing to repeat», source.tell() — here + len(this))
It seems to interpret the «+» as a repetition symbol in the regexp. I’m wondering how to fix this, since I don’t feel comfortable messing around in the systems Python files.
The text was updated successfully, but these errors were encountered:
Traceback (most recent call last):
File «/usr/src/paperless/src/manage.py», line 11, in
File «/usr/lib/python3.7/site-packages/django/core/management/init.py», line 371, in execute_from_command_line
File «/usr/lib/python3.7/site-packages/django/core/management/init.py», line 365, in execute
File «/usr/lib/python3.7/site-packages/django/core/management/base.py», line 288, in run_from_argv
File «/usr/lib/python3.7/site-packages/django/core/management/base.py», line 335, in execute
output = self.handle(*args, **options)
File «/usr/src/paperless/src/documents/management/commands/document_consumer.py», line 96, in handle
File «/usr/src/paperless/src/documents/management/commands/document_consumer.py», line 129, in loop_inotify
File «/usr/src/paperless/src/documents/management/commands/document_consumer.py», line 121, in loop_step
File «/usr/src/paperless/src/documents/consumer.py», line 112, in consume_new_files
if not self.try_consume_file(file):
File «/usr/lib/python3.7/contextlib.py», line 74, in inner
return func(*args, **kwds)
File «/usr/src/paperless/src/documents/consumer.py», line 158, in try_consume_file
File «/usr/src/paperless/src/documents/consumer.py», line 228, in _store
relevant_tags = set(list(Tag.match_all(text)) + list(file_info.tags))
File «/usr/src/paperless/src/documents/models.py», line 84, in match_all
File «/usr/src/paperless/src/documents/models.py», line 108, in matches
if re.search(r»\b<>\b».format(word), text, **search_kwargs):
File «/usr/lib/python3.7/re.py», line 183, in search
return _compile(pattern, flags).search(string)
File «/usr/lib/python3.7/re.py», line 286, in _compile
p = sre_compile.compile(pattern, flags)
File «/usr/lib/python3.7/sre_compile.py», line 764, in compile
p = sre_parse.parse(p, flags)
File «/usr/lib/python3.7/sre_parse.py», line 930, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
File «/usr/lib/python3.7/sre_parse.py», line 426, in _parse_sub
not nested and not items))
File «/usr/lib/python3.7/sre_parse.py», line 651, in _parse
source.tell() — here + len(this))
re.error: nothing to repeat at position 2
Thanks for reporting this @kj21. I think I found a fix and opened PR #571 for this. We’ll let you know once it is fixed!
error: nothing to repeat at position 0 #66
I’m trying to open a seq file using the following code:
The first two lines are fine but when I try and process the final line I get the following error:
error Traceback (most recent call last)
Input In , in ()
—-> 1 img_split.process([‘./Ply4 — Constant Direction_Tape0004.SEQ’])
File path\RHP\lib\site-packages\flirpy\io\seq.py:144, in Splitter.process(self, file_list)
143 logger.info(«Splitting <> into <>«.format(seq, folder))
—> 144 self._process_seq(seq, folder)
146 # Batch export meta data
147 if self.export_meta:
File path\RHP\lib\site-packages\flirpy\io\seq.py:197, in Splitter._process_seq(self, input_file, output_subfolder)
193 def _process_seq(self, input_file, output_subfolder):
195 logger.debug(«Processing <>«.format(input_file))
—> 197 for count, frame in enumerate(tqdm(Seq(input_file, self.height, self.width))):
199 if frame.meta is None:
200 self.frame_count += 1
File path\RHP\lib\site-packages\tqdm\std.py:1195, in tqdm.iter(self)
1192 time = self._time
-> 1195 for obj in iterable:
1196 yield obj
1197 # Update and possibly print the progressbar.
1198 # Note: does not call self.update(1) for speed optimisation.
File path\RHP\lib\site-packages\flirpy\io\seq.py:79, in Seq.getitem(self, index)
76 offset, chunksize = self.pos[index]
77 chunk = self.seq_blob[offset:offset+chunksize]
—> 79 return Fff(chunk, self.width, self.height)
File path\RHP\lib\site-packages\flirpy\io\fff.py:34, in Fff.init(self, data, width, height)
32 self.height = height
33 self.meta = <>
—> 34 self._find_data_offset_simple(width, height)
File path\RHP\lib\site-packages\flirpy\io\fff.py:91, in Fff._find_data_offset_simple(self, width, height)
86 def _find_data_offset_simple(self, width, height):
87 search = struct.pack(» 91 valid = re.compile(search)
92 res = valid.search(self.data)
94 self.data_offset = res.end() + 14
File C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\re.py:252, in compile(pattern, flags)
250 def compile(pattern, flags=0):
251 «Compile a regular expression pattern, returning a Pattern object.»
—> 252 return _compile(pattern, flags)
File C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\re.py:304, in _compile(pattern, flags)
302 if not sre_compile.isstring(pattern):
303 raise TypeError(«first argument must be string or compiled pattern»)
—> 304 p = sre_compile.compile(pattern, flags)
305 if not (flags & DEBUG):
306 if len(_cache) >= _MAXCACHE:
307 # Drop the oldest item
File C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\sre_compile.py:764, in compile(p, flags)
762 if isstring(p):
763 pattern = p
—> 764 p = sre_parse.parse(p, flags)
766 pattern = None
File C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\sre_parse.py:948, in parse(str, flags, state)
945 state.str = str
—> 948 p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
949 except Verbose:
950 # the VERBOSE flag was switched on inside the pattern. to be
951 # on the safe side, we’ll parse the whole thing again.
952 state = State()
File C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\sre_parse.py:443, in _parse_sub(source, state, verbose, nested)
441 start = source.tell()
442 while True:
—> 443 itemsappend(_parse(source, state, verbose, nested + 1,
444 not nested and not items))
445 if not sourcematch(«|»):
File C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.2800.0_x64__qbz5n2kfra8p0\lib\sre_parse.py:668, in _parse(source, state, verbose, nested, first)
666 item = None
667 if not item or item is AT:
—> 668 raise source.error(«nothing to repeat»,
669 source.tell() — here + len(this))
670 if item in _REPEATCODES:
671 raise source.error(«multiple repeat»,
672 source.tell() — here + len(this))
error: nothing to repeat at position 0
exif tools tells me it is:
File Permissions : -rw-rw-rw-
File Type : SEQ
File Type Extension : seq
MIME Type : image/x-flir-seq
Creator Software : CAMCTRL
Camera Model : FLIR SC325
Raw Thermal Image Width : 320
Raw Thermal Image Height : 246
Raw Thermal Image Type : TIFF
Raw Thermal Image : (Binary data 157644 bytes, use -b option to extract)
(and a bunch of stuff I don’t think is relevant)
Is this another case of the endiness biting me? This is the first thing I’ve done with a thermal camera so apologies if I’ve missed anything obvious.
The text was updated successfully, but these errors were encountered:
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:
Why is it raised? 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 stack two regex quantifiers on top of each other. For example, the regex pattern ‘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:
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? Having a single quantifier a+ already reads as “find all matches where at least one character ‘a’ matches”.
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.
[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 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
*’ 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 meta-characters 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 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?
|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||Match regular expression A exactly m times|
|A||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 ? , * , + ,
Let’s have a look at some examples—one for each quantifier:
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.
Alternative Error Message (Fragments)
You may encounter any of the following fragments that all lead to the multiple repeat error:
- re.error: multiple repeat at position
- multiple repeat at position
- sre_constants.error: multiple repeat
- python regex multiple repeat
- python re multiple repeat
- regex multiple repeat
- re.error multiple repeat at position
Again, you can fix the multiple repeat error by avoiding to stack two regex quantifiers on top of each other. For example, the regex pattern ‘a++’ will cause the multiple repeat error—use a single quantifier such as ‘a+’ instead.
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.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.