I have two strings and whenever they are found in the testfile.txt, I wish the part of the string followed by '#' to be truncated up until the end of the string (indicated by ']') and to be replaced by '[]'
The two strings are:
x_123_ABC_def
x_456_ABC_def
The testfile.txt has the following text:
aaaa
bbbbbbb
ccccc
xref:a_sample_file_123_A.html#x_123_ABC_def[A Sample of_123]
ddddddd
eeeeeeee
See xref:a_sample_file_456_A.html#x_456_ABC_def[A Sample_of 456]
ffffffff
See xref:a_sample_file_123_A.html#x_123_ABC_def[A Sample of_123] and xref:a_sample_file_456_A.html#x_456_ABC_def[A Sample_of 456].
ggggggggg
hhhhhhhhh
After running the conversion, I want to see the following:
aaaa
bbbbbbb
ccccc
xref:a_sample_file_123_A.html[]
ddddddd
eeeeeeee
See xref:a_sample_file_456_A.html[]
ffffffff
See xref:a_sample_file_123_A.html[] and xref:a_sample_file_456_A.html[].
ggggggggg
hhhhhhhhh
Here is the code I was trying to use. However, I have no idea how to do the regex part to achieve the above result:
searchtexts = { 'x_123_ABC_def', 'x_456_ABC_def'}
for searchtext in searchtexts:
with open('testfile.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()
for i in range(len(lines)):
line = lines[i]
# Do search replace of searchtext in line here and replace the lines[i] value with it
print (re.sub('????','????',line))
with open('testfile.txt', 'w', encoding='utf-8') as file:
for line in lines:
file.write (line)