Notepad ++搜索并替换多个文本行

时间:2015-12-26 23:09:03

标签: regex notepad++

我在许多不同的规则规则中有数以千计这些规则

edit 1698

set src-address "172.29.44.51/32"

set dst-address "172.29.44.67/32" 

set service "tcp-1022"

set service "tcp-1023"

set service "tcp-1090"

set service "tcp-1098-1102"

set service "tcp-115"

set service "tcp-123"

set service "tcp-13000"

set service "tcp-13001"

set service "tcp-14000"

set service "tcp-14001"

set service "tcp-15000"

set service "tcp-16000"

set service "tcp-1812"

set service "tcp-22"

set service "tcp-32770-32900"

set service "tcp-3307"

set service "tcp-3528"

set service "tcp-3600"

set service "tcp-4400-4500"

set service "tcp-4712-4715"

set service "tcp-5000"

set service "tcp-5000-5630"

set service "tcp-50023-50030"

set service "tcp-5632-7000"

set service "tcp-636"

set service "tcp-7500"

set service "tcp-7600"

set service "tcp-7650"

set service "tcp-7651"

set service "tcp-7900"

set service "tcp-8009"

set service "tcp-8093"

set service "tcp-8443"

set service "tcp-9000"

set service "tcp-9001"

set service "udp-1023"

set service "udp-1102"

set service "udp-123"

set service "udp-13001"

set service "udp-14000"

set service "udp-14000-14001"

set service "udp-161"

set service "udp-162"

set service "udp-177"

set service "udp-22"

set service "udp-389"

set service "udp-45600-45800"

set service "udp-5900"

set service "udp-7500"

set service "udp-8443"

set service "udp-9001"

set service "udp-920-940"

exit 

我希望它将所有设置的服务行收集到一行中,就像这个例子一样

set service "PING" "tcp-1022" "tcp-1023" "tcp-1090" "tcp-1098-1102" "tcp-115" "tcp-123" "tcp-13000" "tcp-13001" "tcp-14000" "tcp-14001" "tcp-15000" "tcp-16000" "tcp-1812" "tcp-22" "tcp-32770-32900" "tcp-3307" "tcp-3528" "tcp-3600" "tcp-4400-4500" "tcp-4712-4715" "tcp-5000" "tcp-5000-5630" "tcp-50023-50030" "tcp-5632-7000" "tcp-636" "tcp-7500" "tcp-7600" "tcp-7650" "tcp-7651" "tcp-7900" "tcp-8009" "tcp-8093" "tcp-8443" "tcp-9000" "tcp-9001" "udp-1023" "udp-1102" "udp-123" "udp-13001" "udp-14000" "udp-14000-14001" "udp-161" "udp-162" "udp-177" "udp-22" "udp-389" "udp-45600-45800" "udp-5900" "udp-7500" "udp-8443" "udp-9001" "udp-920-940"

1 个答案:

答案 0 :(得分:0)

这是一个仅限记事本的++解决方案。它使用的是宏,因为你说你有许多不同的规则文件。

  1. 宏 - >开始录制,现在所有内容都录制到宏
    1. 首先替换以标记设置服务块的开始:
      • 按Ctrl-Pos1,转到当前文件的开头。
      • 搜索 - >替换...:
        查找内容^(?!set service).+?\r\nset service
        替换为\1first set service\3 检查左下角的正则表达式
      • 按全部替换
    2. Second替换以使用起始行加入设置的服务块:
      • 按Ctrl-Pos1,转到当前文件的开头。
      • 搜索 - >替换...:
        查找内容\r\nset service
        替换为:``(无) 检查左下方的扩展
      • 按全部替换
    3. 第三次替换以删除起始行中的标记:
      • 按Ctrl-Pos1,转到当前文件的开头。
      • 搜索 - >替换...:
        查找内容^first set service
        替换为set service 检查左下角的正则表达式
      • 按全部替换
  2. 宏 - >停止录制
  3. 宏 - >保存当前录制的宏,为其命名并为其创建键盘快捷键。
  4. 测试宏。如果有效,请继续,否则将其删除并重新开始录制。
  5. 现在您可以打开所有规则文件并将宏应用于每个文件。 如果您需要删除宏,可以在Macro下找到它 - >修改快捷方式/删除宏。

    修改:这是一个屏幕截图,用于调试您在评论中写下的零匹配问题:screenshot with a part of the regexp,请注意标记的部分:

      查找表达式中的
    • \r\n
    • Dos \ Windows作为状态栏中的lineending类型
    • 左下角检查正则表达式
    • 与文件区域中的搜索匹配的区域

    必须修改行数

    The number of lines have to be modified

    建议的搜索结果Regex

    The result of suggested search Regex