正则表达式删除两个字符串之间的所有字符串?

时间:2020-05-13 13:27:09

标签: regex bash text notepad++

我想删除目录中所有.txt文件的其他两个字符串之间的所有字符串。该怎么做?

.txt文件的内容:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr

[b]Download[/b]
[b][url=https://www.example.com]File.mp4[/url][/b]

[img]https://www.example.com/preview.jpg[/img]

Size: 640 MB | Resolution: 1280x720 | Runtime: 00:15:20 | Format: mp4

Tag1, Tag2, Tag3, Tag4, Tag5, Tag6, Tag7, Tag8

[b]Download[/b]
[b][url=https://www.example.com]File.mp4[/url][/b]

现在我想删除以下之间的所有内容:

“格式:mp4”和[b]下载[/ b]

因此最终输出应如下所示:

Lorem ipsum dolor sit amet, consetetur sadipscing elitr

[b]Download[/b]
[b][url=https://www.example.com]File.mp4[/url][/b]

[img]https://www.example.com/preview.jpg[/img]

Size: 640 MB | Resolution: 1280x720 | Runtime: 00:15:20 | Format: mp4

[b]Download[/b]
[b][url=https://www.example.com]File.mp4[/url][/b]

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

对于记事本++:

Ctrl + H

enter image description here

确保选中正则表达式框。

我们使用的正则表达式执行以下操作:

(?<=Format: mp4)([^[]+)

(?<=Format: mp4)       - Starting after Format: mp4 (but not capturing)
([^[]+)                - Match all characters until [ 

我们将其替换为两个换行符。

答案 1 :(得分:0)

您可以使用sed

sed -i.bak '/Format: mp4$/,/^\[b\]Download/{/Format: mp4$/!{/^\[b\]Download/!d}}' *.txt

结果完全符合您的要求,因此在[b]Download[/b]上方缺少一条空行