Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Regexp question,get 2nd match

by cztmonk (Monk)
on Jul 24, 2012 at 09:52 UTC ( #983342=perlquestion: print w/replies, xml ) Need Help??
cztmonk has asked for the wisdom of the Perl Monks concerning the following question:

Good Morning Monks. I have a logfile containing

bla bla IP address:port bla bla IP address:port

I have this code to get the 1st IP/port. How do I get the 2nd one?

use strict; use warnings; use autodie; my @ipaddress; my $filename = "router.log"; my $myip = "10.0.0."; my $count = 0; open my $f, "<", $filename; while(<$f>){ $count++; while (/$myip/g){ if (m/(\d+\.\d+\.\d+\.\d+\:\d+)/){ push @ipaddress,$1; } } } close $f;

Replies are listed 'Best First'.
Re: Regexp question,get 2nd match
by johngg (Abbot) on Jul 24, 2012 at 10:15 UTC

    Please clarify whether you want all the IP addresses on a line that contains an address on your 10.0.0 network or whether you only want to capture addresses on that network. Note that when you look for a line containing "10.0.0" you should quotemeta your $myip variable because the regex /10.0.0/ will also match 100000 and 10a0b0 since the dot will match any character.



      I want all IP addresses / ports who are accessing 10.0.0.z Thanks for the quotemeta tip, I did not know this one... Regards.

Re: Regexp question,get 2nd match
by davido (Archbishop) on Jul 24, 2012 at 11:12 UTC

    Assuming the IP address will always have a port number appended to it:

    use Regexp::Common qw( net ); while ( <DATA> ) { while ( m/($RE{net}{ipv4}:\d+)/g ) { print $1, "\n"; } } __DATA__ [LAN access from remote] from to, +Monday, July 23,2012 19:42:26

    If you're only interested in those IP's that access "10.0.0.x:y", this will do it:

    while( m/(10\.0\.0\.\d+:\d+)/ ) { print $1, "\n"; }

    If you want something else then let's back up and discuss what you're trying to accomplish first.


Re: Regexp question,get 2nd match
by Corion (Pope) on Jul 24, 2012 at 09:57 UTC

    Maybe consider matching the first IP address and then something and then the second IP address? It shouldn't be hard to modify your regular expression to allow for that.

Re: Regexp question,get 2nd match
by Ratazong (Monsignor) on Jul 24, 2012 at 10:56 UTC

    If (\d+\.\d+\.\d+\.\d+\:\d+) matches one ip-address, then (\d+\.\d+\.\d+\.\d+\:\d+)(\d+\.\d+\.\d+\.\d+\:\d+) will match two(*). The only thing you have to consider that there are some other characters in between. You may get them with .*?. So your final code could be:

    if (m/(\d+\.\d+\.\d+\.\d+\:\d+).*?(\d+\.\d+\.\d+\.\d+\:\d+)/){ push @ipaddress,$2;
    HTH, Rata

    update: (*) well, not exactly, as there is a seperator missing between both (and the regex-engine cannot determine where ip1 ends and where ip2 starts) ... but that is taken care off in the next step...

Re: Regexp question,get 2nd match
by desemondo (Hermit) on Jul 24, 2012 at 09:59 UTC
    Also, you are far more likely to get a helpful response by providing actual source input data.

      Here is my source input

      [LAN access from remote] from to, Monday, July 23,2012 19:42:26

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://983342]
Approved by marto
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2018-01-22 01:32 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (230 votes). Check out past polls.