【问题标题】:Notepad++ :: Remove All Lines And Text That NOT ContainsNotepad++ :: 删除所有不包含的行和文本
【发布时间】:2017-08-20 19:36:20
【问题描述】:

标题几乎是不言自明的......

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

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

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

那是一个文本文件的样本:

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].*).+$

【问题讨论】:

标签: text ip editor notepad++


【解决方案1】:

您可以使用(\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}$1\n:)

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

【讨论】:

  • 像魅力一样工作 8)
猜你喜欢
  • 2014-06-21
  • 2017-03-05
  • 2015-07-15
  • 1970-01-01
  • 1970-01-01
  • 2020-05-11
  • 1970-01-01
  • 2013-11-04
  • 2018-03-15
相关资源
最近更新 更多