0

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)
didjek
  • 159
  • 1
  • 14
  • Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/questions/4736) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Oct 28 '20 at 11:41

0 Answers0