如何替换文本文件中每一行的内容?

时间:2017-05-31 06:30:07

标签: notepad++

我在Notepad ++文件中有一堆具有以下格式的行。

当前格式如下:

Line 1:     2017-05-26 02:26:40,362 Executing SQL query [ Select * from T1 ]
Line 2:     2017-05-26 02:26:40,362 Executing SQL query [ Select * from T2 ] 
Line 3:     2017-05-26 02:26:40,362 Executing SQL query [ Select * from T3 ] 
Line 4:     2017-05-26 02:26:40,362 Executing SQL query [ Select * from T1 ] 

替换后每行的预期结果如下:

Select * from T1 
Select * from T2 
Select * from T3 
Select * from T1 

我也需要以下格式的预期结果:

2017-05-26 02:26:40,362     Select * from T1 
2017-05-26 02:26:41,367     Select * from T2 
2017-05-26 02:26:45,762     Select * from T3 

如何在Notepad ++中实现它?

2 个答案:

答案 0 :(得分:0)

您可以在替换窗口(^.*\[ | \].*$)中用空字符串替换正则表达式Ctrl+H的匹配项。只需确保将搜索模式设置为Regular expression

答案 1 :(得分:0)

import re

txt = '''
Line 1:     2017-05-26 02:26:40,362 Executing SQL query [ Select * from T1 ]
Line 2:     2017-05-26 02:26:40,362 Executing SQL query [ Select * from T2 ]
Line 3:     2017-05-26 02:26:40,362 Executing SQL query [ Select * from T3 ]
Line 4:     2017-05-26 02:26:40,362 Executing SQL query [ Select * from T1 ]
'''

txt_new = [re.findall(r'(Select \* from T[0-9]+)', i)[0] for i in txt.split('\n') if i]
print(txt_new)