use strict;
my $str = 'sh cdp nei
Capability Codes: R - Router, T - Trans-Bridge, B - Source-Route-Bridge
S - Switch, H - Host, I - IGMP, r - Repeater,
V - VoIP-Phone, D - Remotely-Managed-Device,
s - Supports-STP-Dispute
Device-ID Local Intrfce Hldtme Capability Platform Port ID
CHOD-MGMT-STACK.mhmp
mgmt0 129 S I WS-C2960X-48T Gig1/0/25
SPINE-401(FDO22041YMP)
Eth1/49 153 R S s N9K-C9336PQ Eth1/36
SPINE-402(FDO22081W5Y)
Eth1/50 176 R S s N9K-C9336PQ Eth1/36
IPN-2-CH(FDO221610MG)
Eth1/51 125 R S s N9K-C93180YC- Eth1/51
IPN-2-CH(FDO221610MG)
Eth1/52 125 R S s N9K-C93180YC- Eth1/52
IPN-1-KCP(FDO22152M53)
Eth1/54 143 R S s N9K-C93180YC- Eth1/54
Total entries displayed: 6
IPN-1-CH# sh lldp nei
Capability codes:
(R) Router, (B) Bridge, (T) Telephone, (C) DOCSIS Cable Device
(W) WLAN Access Point, (P) Repeater, (S) Station, (O) Other
Device ID Local Intf Hold-time Capability Port ID
SPINE-401 Eth1/49 120 BR Eth1/36
SPINE-402 Eth1/50 120 BR Eth1/36
IPN-2-CH Eth1/51 120 BR Ethernet1/51
IPN-2-CH Eth1/52 120 BR Ethernet1/52
IPN-1-KCP Eth1/54 120 BR Ethernet1/54
Total entries displayed: 5';
my $regex = qr/^(?P<hostname>[\w-_]+)\s+(?P<localInterface>[A-Za-z]+\d+(?:\/\d+)*)\s+(?P<holdTime>\d+)\s+(?P<capabilities>[A-Z]+)\s+(?P<remoteInterface>[A-Za-z]+\d+(?:\/\d+)*) /mp;
if ( $str =~ /$regex/g ) {
print "Whole match is ${^MATCH} and its start/end positions can be obtained via \$-[0] and \$+[0]\n";
# print "Capture Group 1 is $1 and its start/end positions can be obtained via \$-[1] and \$+[1]\n";
# print "Capture Group 2 is $2 ... and so on\n";
}
# ${^POSTMATCH} and ${^PREMATCH} are also available with the use of '/p'
# Named capture groups can be called via $+{name}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Perl, please visit: http://perldoc.perl.org/perlre.html