Regex error: nothing to repeat at position 0

Chandan Malla

I am trying to parse a text in which my regex fails with the error mentioned in the subject.

In the code below I am only using that part, I don't know why it fails! any help would be appreciated

Though I have seen this error in the SO threads but this seems to be different to me

import re
t = '++cnt;'

re.sub('++cnt','@'.join(c for c in '++cnt'),t)

error                                     Traceback (most recent call last)
<ipython-input-482-2b724235a79b> in <module>
      1 t = '++cnt;'
      2 
----> 3 re.sub('+cnt','@'.join(c for c in '++cnt'),t)

~/anaconda3/lib/python3.8/re.py in sub(pattern, repl, string, count, flags)
    208     a callable, it's passed the Match object and must return
    209     a replacement string to be used."""
--> 210     return _compile(pattern, flags).sub(repl, string, count)
    211 
    212 def subn(pattern, repl, string, count=0, flags=0):

~/anaconda3/lib/python3.8/re.py 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:

~/anaconda3/lib/python3.8/sre_compile.py in compile(p, flags)
    762     if isstring(p):
    763         pattern = p
--> 764         p = sre_parse.parse(p, flags)
    765     else:
    766         pattern = None

~/anaconda3/lib/python3.8/sre_parse.py in parse(str, flags, state)
    946 
    947     try:
--> 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

~/anaconda3/lib/python3.8/sre_parse.py 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("|"):

~/anaconda3/lib/python3.8/sre_parse.py in _parse(source, state, verbose, nested, first)
    666                 item = None
    667             if not item or item[0][0] is AT:
--> 668                 raise source.error("nothing to repeat",
    669                                    source.tell() - here + len(this))
    670             if item[0][0] in _REPEATCODES:

error: nothing to repeat at position 0


bb1

In regex + means that the preceding match group needs to be repeated one or more times. You placed + at the beginning of the match pattern so there nothing that can be repeated. It seems that you want to match the character +. If so, then + needs to be escaped, and your match pattern should be '\+\+cnt'

With this change, re.sub(r'\+\+cnt','@'.join(c for c in '++cnt'),t) will have no effect though since if t='++cnt;' then '@'.join(c for c in '++cnt'),t) will give the string '+@+@c@n@t;' which does not contain '++cnt'.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regex Error: Nothing to Repeat

From Dev

regex error - nothing to repeat in jquery

From Dev

when using read_csv I am getting sre_constants.error: nothing to repeat at position 0

From Dev

Pandas Python Regex : error: nothing to repeat

From Dev

Angular nothing to repeat - regex

From Dev

"Nothing to repeat" from Python regex

From Dev

JavaScript RegExp error - Nothing to repeat

From Dev

Regex - I keep getting "Nothing to repeat" exception

From Dev

sre_constants.error: nothing to repeat

From Dev

Javascript regular expression nothing to repeat error

From Dev

Regex number starting with 0 or allow nothing at all

From Dev

There is no row at position 0 error

From Dev

Javascript regexp error (Invalid regular expression: /?/: Nothing to repeat)

From Dev

Python 3.3.6 re.compsre_constants.error: nothing to repeat

From Dev

There is no row at position 0 error TableAdapter

From Dev

regular expression nothing to repeat

From Dev

Spark Cloudant error: 'nothing was saved because the number of records was 0!'

From Dev

There is no row at position 0:c# code error

From Dev

There is no row at position 0:c# code error

From Dev

Regex to match a number or nothing

From Dev

Java regex matches nothing

From Dev

Regex something or nothing

From Dev

PHP Regex Match nothing

From Dev

Repeat block of regex in regex

From Dev

gather with tidyr: position must be between 0 and n error

From Java

Error: onActivityResult overrides nothing

From Dev

Rescuing error does nothing?

From Dev

Error Handling to do nothing

From Dev

Python Regular Expression returning nothing to repeat?