Home > OS >  error: nothing to repeat at position 0 using regex in python
error: nothing to repeat at position 0 using regex in python

Time:12-11

Given a censored string and a string of the censored vowels, return the original uncensored string.

import re

def uncensor(string1, string2):
  # Use a regular expression to find all the asterisks in string1
  asterisks = re.findall(r'\*?', string1)

  # Replace each asterisk with the corresponding character from string2
  for i, asterisk in enumerate(asterisks):
    string1 = re.sub(asterisk, string2[i], string1, count=1)

  return string1

uncensor("Wh*r* d*d my v*w*ls g*", "eeioeo") #➞ "Where did my vowels go?"

I got the following error:

error                                     Traceback (most recent call last)
<ipython-input-28-fee597a500f6> in <module>
     11   return string1
     12 
---> 13 uncensor("Wh*r* d*d my v*w*ls g*", "eeioeo") #➞ "Where did my vowels go?"

6 frames
/usr/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

I tried pattern r'* *' , r'*', r'* ' but I am always getting error.

i was expecting this

uncensor("Wh*r* d*d my v*w*ls g*", "eeioeo") ➞ "Where did my vowels go?"

CodePudding user response:

There are a few problems here. First of all the use of ? in the search pattern is wrong. If you inspect your results, you will see that asterisks is:

['', '', '*', '', '*', '', '', '*', '', '', '', '', '', '', '*', '', '*', '', '', '', '', '*', '']

So it should just be r"\*".

Secondly, in your replacement loop, the asterisk is the actual match from the string, so your pattern becomes *, which is invalid. You can do re.escape(asterisk) to escape it.

Lastly, you don't even need re here as all your regex gives you is a list of asterisks. The whole task can be done with string operations:

def uncensor(string1, string2):
  # Replace each asterisk with the corresponding character from string2
  for repl in string2:
    string1 = string1.replace('*', repl, 1)

  return string1

uncensor("Wh*r* d*d my v*w*ls g*", "eeioeo") #➞ "Where did my vowels go?"

CodePudding user response:

Your current approach has multiple problems, with the function logic and the regex usage. I would go about this by doing a regex replacement on \* with a callback function. In the callback we can pop one vowel from the list of replacement vowels, for each * occurence.

import re

def uncensor(string1, string2):
    chars = list(string2)
    return re.sub(r'\*', lambda m: chars.pop(0), string1)

output = uncensor("Wh*r* d*d my v*w*ls g*", "eeioeo")
print(output)  # Where did my vowels go
  • Related