Notepad++ Localizar e substituir curinga

Notepad++ Localizar e substituir curinga

Eu li muito sobre como substituir pelo Notepad ++, mas parece que não cheguei a lugar nenhum.

Aqui está um exemplo do Juniper Config que irei mudar para outro idioma:

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

Agora quero mudar isso para a linguagem Fortinet, mas para começar esse processo preciso procurar certas variáveis. Os poucos bits que preciso alterar são:

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

para

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

então usei o seguinte para tentar começar primeiro:

Localizar (set policy id (.+))e substituir por edit 0- Isso parece destacar tudo e me deixar com uma configuração vazia, faltando apenas editar 0.

Então tentei localizar (set policy id *)e substituir por edit 0- Isso me deixa com tudo, mas muda para que fique parecido com o abaixo: (Basicamente insere 0 antes de XXXX

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

Qualquer ajuda seria muito apreciada, pois isso poderia me poupar horas!!!!

Responder1

IMHO, você deve escrever um script em sua linguagem de script favorita.

Mas se você realmente deseja fazer esse trabalho com o Ntepad++, aqui está uma solução regex. Como você pode ver o regex e a substituição são um pouco complexos e requerem um bom conhecimento para serem mantidos.

  • Ctrl+H

  • Encontre o que: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

  • Substituir com: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

  • VERIFICAR Caso de compatibilidade

  • VERIFICAR Envolver em torno

  • VERIFICAR Expressão regular

  • VERIFICAR . matches newline

  • Replace all

  • Find All in Current Document

Explicação:

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

Substituição:

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

Captura de tela (antes):

insira a descrição da imagem aqui

Captura de tela (depois):

insira a descrição da imagem aqui

Você encontrará uma explicação completaaqui

informação relacionada