regexpowershellcisco-ios

Regex returning undesired value


I am trying to create a script that will comb through a Cisco "show run all" file and return an interface based on if a command appears. I have a start of Regex, but it's matching on the previous interface and not the desired interface.

My regex is interface (\S+)(?s).*?access-session port-control force-authorized

Sample information below. The desired result would be GigabitEthernet1/0/45, but instead it is returning GigabitEthernet1/0/44.

Sample Data:

interface GigabitEthernet1/0/44  
access-session port-control auto 
!
interface GigabitEthernet1/0/45  
access-session port-control force-authorized 

Solution

  • Alright, so as I thought I was close, but here is what works. Basically I had to tell it to stop matching at the ! which separates the interface config within the config file. (adding [^!] )

    interface (\S+)(?s)[^!].*?access-session port-control force-authorized