Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: regex multiple lines

by davido (Archbishop)
on Jan 30, 2012 at 19:12 UTC ( #950827=note: print w/replies, xml ) Need Help??


in reply to regex multiple lines

This works for the data set provided. It is probably a little less efficient than a solution that breaks the problem into smaller chunks. But it is possible to do it all with one ugly regex. In this case it becomes easier to use named captures than to try to count $1, $2, etc. And the /x option helps to make it more readable.

use strict; use warnings; my $string = <<HERE; MAC Address: xx:aa:aa:aa:aa:aa IP addr: a.b.c.d x.y.z.f Name: xxxx Neighbors attached: 1 Additional: xxx Interface: xxx Index: xxx Link Cost: xx Link Speed: xx Mbps MAC Address: xx:aa:aa:aa:aa:aa IP addr: a.b.c.d x.y.z.f Name: xxxx Neighbors attached: 1 Additional: xxx Interface: xxx Index: xxx Link Cost: xx Link Speed: xx Mbps HERE while( $string =~ m/ MAC\sAddress:\s(?<mac>\S+)\s+ IP\saddr:\s(?<ip1>\S+)\s+(?<ip2>\S+) .+? Link\sCost:\s(?<cost>\S+)\s+ Link\sSpeed:\s(?<speed>\S+)\sMbps /sxg ) { print "Node with MAC address: $+{mac} has IP = $+{ip1}, $+{ip2} " +. "and Link Speed = $+{speed} and Link Cost = $+{cost}\n"; }

...the output...

Node with MAC address: xx:aa:aa:aa:aa:aa has IP = a.b.c.d, x.y.z.f and + Link Speed = xx and Link Cost = xx Node with MAC address: xx:aa:aa:aa:aa:aa has IP = a.b.c.d, x.y.z.f and + Link Speed = xx and Link Cost = xx

Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://950827]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-03-23 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (279 votes). Check out past polls.