I am trying to process user input in regex from variable. After a lot of searching I have come up with following:
Explaination of code variables:
step is a string used as input for regex
e.g.
replace|-|space ,
replace|*|null,
replace|/|\|squot|space
b is a list of elements. Element is fetched and modified as per regex.
i is integer received from other function to access list b using i as index
I process the above string to get array, then use the last element of array as substitution string
First element is deleted as it is not required. All other elements need to be replaced with substitution string.
def replacer(step,i,b):
steparray = step.split('|')
del steparray[0]
final = steparray.pop()
if final == "space":
subst = u" "
elif final == "squot":
subst = u"'"
elif final == "dquot":
subst = u"\""
else:
subst = u"%s"%final
for input in xrange(0,len(steparray)):
test=steparray[input]
regex = re.compile(ur'%s'%test)
b[i] = re.sub(regex, subst, b[i])
print b[i]
However, when I run above code, following error is shown:
File "CSV_process.py", line 78, in processor
replacer(step,i,b)
File "CSV_process.py", line 115, in replacer
regex = re.compile(ur'%s'%test)
File "/usr/lib/python2.7/re.py", line 190, in compile
return _compile(pattern, flags)
File "/usr/lib/python2.7/re.py", line 242, in _compile
raise error, v # invalid expression
sre_constants.error: nothing to repeat
I tried a lot but dont understand how regex works. Please help with error.
Final requirement is to get a special character from user input and replace it with another character (again from user input)
PS: Also, the code does not have 242 lines but error is on line 242. Is the error occurring after end of array in for loop?
testbegins with*.*? I have not allowed*as input but the b[i] may contain*