Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
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


Comment on Re: regex multiple lines
Select or Download Code

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 surveying the Monastery: (10)
As of 2014-09-18 15:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (116 votes), past polls