Home > Net >  Regular expression for Cisco enable prompt
Regular expression for Cisco enable prompt

Time:04-30

I am looking for a generic regular expression that can match with any of these enable prompts,

RP/0/RP0/CPU0:ios#
RP/0/RP0/CPU0:R1#
RP/0/RP0/CPU0:CX1-SF#
RP/0/RP0/CPU0:~#
RP/0/RP0/CPU0:cisco#

and should not match with config prompts like these,

RP/0/RP0/CPU0:ios(config)#
RP/0/RP0/CPU0:R1(config)#
RP/0/RP0/CPU0:R1(config-if)#

I started with something like this,

(.*?)RP/\d /RP[01]/CPU\d :. #\s*?

But it is also matching with config prompts,

https://regexr.com/6km9a

CodePudding user response:

Use a negative lookbehind before the # that prohibits ).

(.*?)RP/\d /RP[01]/CPU\d :. (?<!\))#\s*?

CodePudding user response:

You could omit matching parenthesis or a newline right after the colon using a negated character class like [^()\n]* and then matching until till the #

Note that you can omit the \s*? as it is the last part of the pattern and non greedy, so it will not match any characters.

(.*?)\bRP/\d /RP[01]/CPU\d :[^()\n]*#

See a regex101 demo.

Or you could exclude matching "config" between parenthesis:

(.*?)\bRP/\d /RP[01]/CPU\d :(?!\w \(config\b[^()\n]*\)). #

See another regex101 demo.

  • Related