Notepad++ 查找并替换通配符

Notepad++ 查找并替换通配符

我已经阅读了大量关于如何用 Notepad++ 替换的资料,但似乎毫无进展。

下面是 Juniper Config 的示例,我将把它转换成另一种语言:

set policy id 3016 from "CSA" to "Untrust"  "1.1.1.1/32" "2.2.2.2/32" "SSH" permit log 
set policy id 3016
set dst-address "3.3.3.3/32"
exit
!    
set policy id 3053 name "Footprints" from "Untrust" to "CSA"  "Blocked Addresses" "Any" "ANY" deny log 
set policy id 3053
exit

现在我想将其更改为 Fortinet 语言,但为了开始此过程,我需要它查找某些变量。我需要更改的几个位是:

set policy id 3016 from "CSA" to "Untrust"  "1.1.1.1/32" "2.2.2.2/32" "SSH" permit log 
set policy id 3016
set dst-address "3.3.3.3/32"
exit
!    
set policy id 3053 name "Footprints" from "Untrust" to "CSA"  "Blocked Addresses" "Any" "ANY" deny log 
set policy id 3053
exit

edit 0
    set srcintf "CSA"
    set dstintf "Untrust"
    set srcaddr "1.1.1.1/32"
    set dstaddr "2.2.2.2/32" "3.3.3.3/32"
    set action accept
    set schedule "always"
    set service "SSH"
    set fsso disable
    set nat enable
next
edit 0
    set name "Footprints"
    set srcintf "Untrust"
    set dstintf "CSA"
    set srcaddr "Blocked Addresses"
    set dstaddr "all"
    set action accept
    set schedule "always"
    set service "ALL"
    set fsso disable
    set nat enable
next

因此我首先尝试使用下面的方法:

查找(set policy id (.+))并替换edit 0- 这似乎突出显示了所有内容,并给我留下了空配置,只剩下编辑 0。

然后我尝试查找(set policy id *)并替换edit 0- 这让我保留了所有内容,但更改了它,因此它看起来像下面这样:(基本上在 XXXX 之前插入 0

edit 03016 from "CSA" to "Untrust" "1.1.1.1/32" "2.2.2.2/32" "SSH" permit log set policy id 3016 set dst-address "3.3.3.3/32" exit

任何帮助都将不胜感激,因为这可以节省我很多时间!!!

答案1

恕我直言,你应该用你最喜欢的脚本语言编写一个脚本。

但如果你真的想用 Ntepad++ 完成这项工作,这里有一个正则表达式解决方案。正如你所见,正则表达式和替换有点复杂,需要很好的知识才能维护。

  • Ctrl+H

  • 找什么:set policy id \d+ (?:(?:(?!exit).)*?name (".+?"))?(?:(?!exit).)*?from (".+?")(?:(?!exit).)*?to (".+?")(?:(?!exit).)*?("\d{1,3}(?:\.\d{1,3}){3}/\d+"|"[\w ]+") ("\d{1,3}(?:\.\d{1,3}){3}/\d+"|"[\w ]+") (".+?")(?:(?:(?!exit).)*?set dst-address (".+?"))?(?:(?!exit).)+?exit

  • 用。。。来代替:edit 0\n\t(?1set name $1\n\t:)set srcintf $2\n\tset dstintf $3\n\tset srcaddr $4\n\tset dstaddr $5(?7 $7:)\n\tset action accept\n\tset schedule "always"\n\tset service $6\n\tset fsso disable\n\tset nat enable\nnext

  • 查看 相符

  • 查看 环绕

  • 查看 正则表达式

  • 查看 . matches newline

  • Replace all

  • Find All in Current Document

解释:

set policy id \d+                           # literally and digits for id
(?:                                         # non capture group
    (?:(?!exit).)*?                         # 0 or more any character but not the word "exit"
    name (".+?")                            # name is captured in group 1
)?                                          # end group, optional
(?:(?!exit).)*?                             # 0 or more any character but not the word "exit"
from (".+?")                                # from is captured in group 2
(?:(?!exit).)*?                             # 0 or more any character but not the word "exit"
to (".+?")                                  # to is captured in group 3
(?:(?!exit).)*?                             # 0 or more any character but not the word "exit"
("\d{1,3}(?:\.\d{1,3}){3}/\d+"|"[\w ]+")    # group 4, src IP or literal like "Blocked Addresses"
("\d{1,3}(?:\.\d{1,3}){3}/\d+"|"[\w ]+")    # group 5, dst IP or literal like "Any"
(".+?")                                     # group 6, service
(?:                                         # non capture group
    (?:(?!exit).)*?                         # 0 or more any character but not the word "exit"
    set dst-address                         # literally
    (".+?")                                 # group 7, 1 or more any character, not greedy
)?                                          # end group, optional
(?:(?!exit).)*?                             # 0 or more any character but not the word "exit"
exit                                        # literally

替代品:

edit 0\n\t                      # edit 0 followed by linefeed and tabulation
(?1                             # if group 1 exists, (name)
    set name $1\n\t               # print the name (group 1)
    :                           # else
                                  # nothing
)                               # end if
set srcintf $2\n\t              # and so on ...
set dstintf $3\n\t
set srcaddr $4\n\t
set dstaddr $5
(?7 $7:)\n\t
set action accept\n\t
set schedule "always"\n\t
set service $6\n\t
set fsso disable\n\t
set nat enable\n
next

屏幕截图(之前):

在此处输入图片描述

屏幕截图(之后):

在此处输入图片描述

你会找到完整的解释这里

相关内容