The import re
The import string
The import OS
The file=open (r 'C: \ Users \ Administrator \ Desktop \ \ new folder aa TXT', 'r')
# # lines=file. Readlines () according to the line read
S=file. The read ()
String="'. Join (s)
Result=re. The.findall (" aaa "+" (. *?) "+" CCC ", s)
# print (result)
While len (result) & gt; 0:
A=result. Pop (0)
DD=(s.r eplace (A, ' ') # to replace the original text is empty
# print (result)
# print (DD)
The save=open (r 'C: \ Users \ Administrator \ Desktop \ \ new folder bb TXT', 'w')
Save. Write (DD)
Save the close ()
===================================================================================
The contents of a text and the resulting text b as shown in figure:

Failed to achieve full replacement, really can't find the reason,,
===================================================================================
Small white at first contact, ask the great spirit guide

CodePudding user response:
The greed of the re in matching greed matching try insteadCodePudding user response:
Even at the same time set up re matching labels: re SCodePudding user response:
The import re
The file=open (r 'C: \ Users \ admin \ Desktop \ aa TXT', 'r')
Lines=file. Readlines () line # according to read
Result=""
For the line in lines:
Result +=re. Sub (r '(aaa) (CCC)' (. *), r ', 1, 3, line, flags.=re S)
The save=open (r 'C: \ Users \ admin \ Desktop \ bb TXT', 'w')
Save. Write (result)
Save the close ()