Infoblox Exchange Cybersecurity Roadshow 2020 – Join us!
North America | Europe | Middle East/Africa | Asia-Pacific

Network Change & Configuration Management

Reply
Accepted Solution

Regex for Cisco Interface Names

[ Edited ]
Authority
Posts: 21
5626     1

Folks,

 

While working with CCS on the NetMRI platform, I noticed that the following Regex:

 

 

/(\w+\d+(\/\d{1,2}|\/\d{1,2}\/\d+|\/\d{1,2}\.\d+|\/\d{1,2}\:\d+)?|\w+-\w+\d{1,3})/

 

would not match an interface named:

 

GigabitEthernet0/0/0.207

 

I modified the regex to:

 

/(\w+\d+(\/\d+\/\d+\:\d+\.\d+|\/\d+\/\d+\/\d+|\/\d+\/\d+\.\d+|\/\d+\.\d+|\/\d+\/\d+|\/\d+)?|\w+\-\w+)/

 

and now it's matching properly for the following test cases:

 

GigabitEthernet0
GigabitEthernet0/1
GigabitEthernet0/1.434
GigabitEthernet0/1/0
GigabitEthernet0/1/0.434
GigabitEthernet0/1/0/0

Bundle-Ether1

Serial0/0/0:0.517

GigabitEthernet323230/43434354/545454230/4545450 (Yeah, that too.)

 

The only problem I seem to run into is with the following:

 

GigabitEthernet0/1/2/3/4 

 

the last "/4" never gets matched. Thankfully, I don't have to worry about it in my current environment, but that doesn't preclude me from finding a solution. 

 

Has anyone come up with a more comprehesive pattern? 

 

Regards,

 

Mark

 

 

Re: Regex for Cisco Interface Names

[ Edited ]
Authority
Posts: 32
5627     1

Maybe something like 

 

\w+(-\w+)?\d+(([\/:]\d+)+(\.\d+)?)?

 
would work?  In Human, that's 

 

Any word, optionally hyphenated with another word, followed by
   some digits, optionally followed by
      one or more repetitions of either a "/" or a ":" followed by digits, optionally followed by
         a "." and some digits.

 
...so it'll match regardless of how many interface "levels" there are, and only allows the "dot subinterface" to be in the last grouping.  So it'll match GigabitEthernet0/1/14.42, but not GigabitEthernet0/1.5/42.

 

That matches all the interface names on your list, including "GigabitEthernet0/1/2/3/4", and would also match "GigabitEthernet0/1/2/3/4/5/6/7/8/9".  :-)

 

It will also match a few constructs that may not technically be legal interface names, since it will allow for digits separated by a colon in any position, not only after the second slash like yours did.  Not sure if that's likely to be an issue?

 

Showing results for 
Search instead for 
Do you mean 

Recommended for You