CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Advanced: Re: [yanivr78] Regex source -> destination line matches...: Edit Log

Veteran / Moderator

Mar 9, 2013, 4:54 AM

Views: 3869
Re: [yanivr78] Regex source -> destination line matches...

Just one simplified solution:

my $line = "Dec 4 20:25:21 %ASA-6-106100: access-list inside permitted tcp inside/ -> pscdmz/ hit-cnt 1 first hit"; 
my $ip = qr/\d+\.\d+\.\d+\.\d+/; #IP regex should be made much more selective, for instance by replacing + by {1,3}, this is just a quick thing for the example
my ($source, $dest) = ($2, $3) if $line =~ /($ip).+\D($ip).+\D($ip)/;
print $source, " ", $dest; # prints

This was just a quick demonstration to show you how to pick up the second and the third IP addresses in the line. I would probably not write my code this way.

A less trivial solution could be to replace the relevant line by:

my ($source, $dest) =  ($line =~ /($ip).+\D($ip).+\D($ip)/)[1, 2];


my ($source, $dest) =  ($line =~ /$ip.+\D($ip).+\D($ip)/)[0, 1];

Or, yet simpler:

my ($source, $dest) =  ($line =~ /$ip.+\D($ip).+\D($ip)/);

And, as said in the comment, the IP regex should be much more elaborated, this was just a quick fix.

In Reply To
1) "use the 3 arg form of open and include the reason it failed in the die statement (i.e., include the $! var in the statement)."

Will it be alright if you take out the example from my code and show here what you mean ? (I need to see it in order to understand)

Something like this:

my $in_file = "my_file.txt"; 
open my $FILE_HANDLER, "<", $in_file or die "Can't open $in_file $!\n";

(This post was edited by Laurent_R on Mar 9, 2013, 5:00 AM)

Edit Log:
Post edited by Laurent_R (Veteran) on Mar 9, 2013, 4:58 AM
Post edited by Laurent_R (Veteran) on Mar 9, 2013, 5:00 AM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives