0

Need help with a regex to select multiple lines. From what I have so far it works until I put the last regex in. Once I do that, it skips over the middle one. Passwords have been removed for security purposes.

My Goals

  1. Select all lines beginning at Current configuration ending at version, "or the ! right before it"
  2. Select all lines with "secret"
  3. Select all lines with "password"

My Regex

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

EDIT: took about spaces before and after the | and it seems to highlight what i want. It doesn't do the entire line though.

Test Data

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
!

Desired Result:

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
!
5
  • are you using global? it seems to be working how you have it if you use the global flag regex101.com/r/TyT1Mp/1 Commented Jan 31, 2017 at 14:45
  • did you try without regex ? Commented Jan 31, 2017 at 14:48
  • Replace (.*) with (.*?) Commented Jan 31, 2017 at 14:52
  • @maksymiuk I removed the spaces before the | and it seems to be selecting what i need now. do you know how to use re.sub(). I'm basically trying to use this regex to then replace it with an empty string " ". Commented Jan 31, 2017 at 15:04
  • Whats the final desired result? Can you also include that? Commented Jan 31, 2017 at 15:06

1 Answer 1

1

This Should Work.

REGEXP:

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

INPUT:

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
!

OUTPUT:

Group: $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***

See: https://regex101.com/r/NOrndn/1

Test Python Code: http://ideone.com/UyjT38

Sign up to request clarification or add additional context in comments.

1 Comment

I i modified the regex to just have secret instead of enable secret and this does exactly what i want. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.