Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
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 the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2016-09-29 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Extraterrestrials haven't visited the Earth yet because:







    Results (544 votes). Check out past polls.