正则表达式多个过滤器

时间:2017-01-31 14:41:31

标签: python regex python-2.7

需要有关正则表达式的帮助才能选择多行。从我到目前为止,它一直有效,直到我把最后一个正则表达式放入。一旦我这样做,它就跳过中间的正则表达式。为安全起见,已删除密码。

我的目标

  1. 选择以当前配置结尾处开始的所有行 版本,"或者!就在它之前"
  2. 使用" secret"
  3. 选择所有行
  4. 使用"密码"
  5. 选择所有行

    我的正则表达式

    (?s)Current configuration (.*)NVRAM|secret 5 +(\S+)|password 7 +(\S+)
    

    编辑:在|之前和之后占用了大约空格它似乎突出了我想要的东西。但它并没有做整行。

    测试数据

    Building configuration...
    
    Current configuration : 45617 bytes
    !
    ! Last configuration change at 00:22:36 UTC Sun Jan 22 2017 by user
    ! NVRAM config last updated at 00:22:43 UTC Sun Jan 22 2017 by user
    !
    version 15.0
    no service pad
    !
    no logging console
    enable secret 5 ***encrypted password***
    !
    username admin privilege 15 password 7 ***encrypted password***
    username sadmin privilege 15 secret 5 ***encrypted password***
    aaa new-model
    !
    ip ftp username ***encrypted password***
    ip ftp password 7 ***encrypted password***
    ip ssh version 2
    !
    line con 0
     password 7 ***encrypted password***
     login authentication maint
    line vty 0 4
     password 7 ***encrypted password***
     length 0
     transport input ssh
    line vty 5 15
     password 7 ***encrypted password***
     transport input ssh
    !
    

    期望的结果:

    Building configuration...
    
    !
    version 15.0
    no service pad
    !
    no logging console
    enable 
    !
    username admin privilege 15 
    username gisadmin privilege 15 
    aaa new-model
    !
    ip ftp username cfgftp
    ip ftp 
    ip ssh version 2
    !
    line con 0
    
     login authentication maint
    line vty 0 4
    
     length 0
     transport input ssh
    line vty 5 15
    
     transport input ssh
    !
    

1 个答案:

答案 0 :(得分:1)

这应该有效。

<强> REGEXP:

((?:\bCurrent configuration)(?:.*\n?){6})$|((?:\bsecret)(?:.)+$)|((?:\bpassword\s)(?:.)+$)

<强> INPUT:

构建配置......

Current configuration : 45617 bytes
!
! Last configuration change at 00:22:36 UTC Sun Jan 22 2017 by user
! NVRAM config last updated at 00:22:43 UTC Sun Jan 22 2017 by user
!
version 15.0
no service pad
!
no logging console
enable secret 5 ***encrypted password***
!
username admin privilege 15 password 7 ***encrypted password***
username sadmin privilege 15 secret 5 ***encrypted password***
aaa new-model
!
ip ftp username ***encrypted password***
ip ftp password 7 ***encrypted password***
ip ssh version 2
!
line con 0
 password 7 ***encrypted password***
 login authentication maint
line vty 0 4
 password 7 ***encrypted password***
 length 0
 transport input ssh
line vty 5 15
 password 7 ***encrypted password***
 transport input ssh
!

<强>输出:

组:$ 1:

Current configuration : 45617 bytes
!
! Last configuration change at 00:22:36 UTC Sun Jan 22 2017 by user
! NVRAM config last updated at 00:22:43 UTC Sun Jan 22 2017 by user
!
version 15.0

GROUP $ 2:

enable secret 5 ***encrypted password***

GROUP $ 3:

password 7 ***encrypted password***
password 7 ***encrypted password***
password 7 ***encrypted password***
password 7 ***encrypted password***
password 7 ***encrypted password***

请参阅: https://regex101.com/r/NOrndn/1

测试Python代码: http://ideone.com/UyjT38

相关问题