Notepad++ :: 删除所有不包含的行和文本

Notepad++ :: Remove All Lines And Text That NOT Contains

标题几乎不言自明...

我的正则表达式仍然不完美,但在改进之前我需要解决 2 个问题...

我可以使用 ip:port 获取所有行,但我不知道如何获取:

  1. 删除该行中的其余文本。
  2. 将空行 \r\n\ 替换为 AT THE SAME REGEX REQUEST。

文本文件示例:

junk text junk text junk text junk text junk text junk text junk text 
junk text 127.0.0.1:28 junk text junk text junk text junk text junk text 
junk text junk text junk text junk 127.0.0.1:28text junk text 
junk text junk text junk text 127.0.0.1:28 junk text 
junk text junk text junk text 
junk text 127.0.0.1:28 junk text 
junk text 
junk text 127.0.0.1:28 junk text 
junk text 127.0.0.1:28 junk text junk text 
junk text junk text junk text 127.0.0.1:28 junk text 
junk text junk text junk text junk text junk text 
junk text junk text junk text 127.0.0.1:28 junk text junk text junk text 
junk text junk text 127.0.0.1:28 junk text junk text junk text junk text junk text 

我期待回来:

127.0.0.1:28
127.0.0.1:28
127.0.0.1:28
127.0.0.1:28
127.0.0.1:28
127.0.0.1:28
127.0.0.1:28
127.0.0.1:28
127.0.0.1:28

显然该示例使用相同的 ip:port 行,但我不希望它是固定值。

是否可以通过 1 个正则表达式请求来完成?

...作为我尝试的起点:

^(?!.*[09].*).+$

您可以使用 (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+) 匹配和捕获 IP,然后您可以使用 (?:(?!\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+).)* tempered greedy token 匹配任何不以 IP 开头的文本,例如子字符串。

查找内容: (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+)|(?:(?!\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+).)*

如果在模式开头添加 (?s),则不必检查 . 匹配换行符 选项。

要仅替换为找到的 IP 并添加换行符,可以使用条件替换模式来完成:

(?{1}\n:)

如果匹配 + 换行符,它将用第 1 组值 (IP) 替换匹配项,否则,匹配项将替换为空字符串。