2017-01-31 100 views
0

需要帮助使用正则表达式来选择多行。从我迄今为止它的工作,直到我把最后一个正则表达式。一旦我这样做,它跳过中间的一个。出于安全目的,密码已被删除。正则表达式多个过滤器

我的目标

  1. 选择位于在 版本结束当前配置开始的所有行“或!右侧前”
  2. 选择的所有生产线的“秘密”
  3. 选择所有行带“密码”

我的正则表达式

(?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 
! 
+0

您使用的是全球性的?它似乎工作如何你有它,如果你使用全球标志https://regex101.com/r/TyT1Mp/1 – maksymiuk

+0

你尝试没有正则表达式? – Saksow

+0

用'(。*?)替换'(。*)')' –

回答

1

这应该工作。

REGEXP:

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

输入:

大楼配置...

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 
! 

OUTPUT:

组:$ 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

+0

我修改了正则表达式只是有秘密而不是启用秘密,这正是我想要的。谢谢。 – NineTail

+0

不客气。 –