I want to use re.sub
to replace a part of a string I know exactly what looks like. relevant part of code:
print "Regex statement: ", foundStatements[iterator]
print "string to replace with : \n", latexPreparedString
print "string to search&replace in: \n", fileAsString
processedString = re.sub(foundStatements[iterator], latexPreparedString, fileAsString)
print "processed string: \n", processedString
In my testing case, foundStatements[iterator]
is "%@import script_example.py ( *out =(.|\n)*?return out)"
But even though processedString
contains foundStatements[iterator]
, processedString
looks exactly like fileAsString
, so it hasn't accomplished the re.sub
task. What am I doing wrong?
EDIT: Ok, it definitely has something to do with the string I'm searching to replace containing regex code. Is there a way to make it just interpret it foundStatements[iterator]
as a raw string to search for? The only solution I can think of is to create a function that replaces any regex symbols in a string with \regexsymbol (e.g. * -> \*), but it'd make sense for there to be a way to solve this with inbuilt functions. It'd also be a bit overkill since I'd have to make sure it works with every single regex symbol, of which there are quite a few :/
EDIT2: Well, just changing it to re.sub(re.escape(foundStatements[iterator]), latexPreparedString, fileAsString)
seems to work. except when the regex statement doesn't hit anything in the original file. To explain, latexPreparedString
is generated by using the regex-part of the foundStatements[iterator]
. While it's logical that it shouldn't be able to set latexPreparedString
to anything when the regex statement doesn't hit anything, I set latexPreparedString = "
" by default, so in that case it should re.sub
replace it with a blank string if it doesn't hit anything. Here's how to code looks at the moment: pastebin.com/wUedK3LN