I only started python recently and have never written any code before. I used a regular expression to match a string in the input file (which was successful) but I am really struggling to find a way to replace that string in the file with another using a regular expression.
with open( fileToSearch, "r+" ) as file: for line in fileinput.input( fileToSearch ): string4=line result1 = re.search(r'(KNOWLEDGECENTER\/.*?\/)' + re.escape(taxonomy), string4) print (result1) result2 = re.sub(result1, r'(KNOWLEDGECENTER\/\t(\1)\/\)' + taxonomy, string4) print (result2) file.write(result2)
I expected that re.sub would replace the string in the result1 variable with the replacement string but instead, I am getting the following error:
raise TypeError, "first argument must be string or compiled pattern" TypeError: first argument must be string or compiled pattern
If I put the result1 variable in quotes in the re.sub statement, as shown below, I don't get an error but the input file doesn't get updated with the replacement string
result2 = re.sub('result1', r'(KNOWLEDGECENTER\/\t(\1)\/\)' + taxonomy, string4)
re.search code appears to work as print (result1) returns: <_sre.SRE_Match object at 0x02A120E0> for each line in the input file