Question

I am trying to determine whether a command I place on a cisco router, only has only neighbor entry inside the following output..

I have the following regex .. How do I get the search to fail if it reaches the end and doesn't find the second match?

if (! $document =~/(Device\sID)(.*?)(Device\sID)/s){        
    print("Theres no double entries\n");
} else { 
    print ("double!\n");
}

Device ID: NAME1
Entry address(es): 
  IP address: IP.IP.IP.IP
Plautform: cisco WS-C3850-48T,  Capabilities: Switch IGMP 
Interface: GigabitEthernet1/x,  Port ID (outgoing port): GigabitEthernetx/x/x
Holdtime : 123 sec

Version :
Cisco IOS Software, IOS-XE Software, Catalyst L3 Switch Software (CAT3K_CAA-UNIVERSALK9-M), Version 03.02.03.SE RELEASE SOFTWARE (fc2)
Technical Support: http://www.cisco.com/techsupport
Copyright (c) 1986-2013 by Cisco Systems, Inc.
Compiled Mon 23-Sep-13 18:24 by prod_rel_team

advertisement version: 2
VTP Management Domain: 'NAME1'
Native VLAN: 1
Duplex: full
Management address(es): 
  IP address: IP.IP.IP.IP

Regards, Peter

Was it helpful?

Solution

You have a problem of operator precedence. ! has an higher precedence than =~, so your expression:

 if ( ! $document =~ /(Device\sID)(.*?)(Device\sID)/s ) {

is the same that:

 if ( (! $document) =~ /(Device\sID)(.*?)(Device\sID)/s ) {

but you want:

 if ( ! ($document =~ /(Device\sID)(.*?)(Device\sID)/s) ) {

that is the same that:

 if ( $document !~ /(Device\sID)(.*?)(Device\sID)/s ) {

OTHER TIPS

!~ is the operator for regex not matchd.

$document !~ /(Device\sID)(.*?)(Device\sID)/s
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top