-2

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


Chandan Malla
  • 481
  • 5
  • 14
  • 1
    `+` is not literal in regex. It means "one or more of the previous character". You have to escape it if you want it literal: `re.sub(r"\+\+cnt", ...`. – ggorlen Feb 27 '21 at 16:05

1 Answers1

0

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'

bb1
  • 7,174
  • 2
  • 8
  • 23