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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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



Laurent_R
Veteran / Moderator

Mar 9, 2013, 4:54 AM


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

Just one simplified solution:


Code
my $line = "Dec 4 20:25:21 10.85.254.10 %ASA-6-106100: access-list inside permitted tcp inside/10.80.20.25(42940) -> pscdmz/155.16.61.33(3181) 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 10.80.20.25 155.16.61.33


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:


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


Or:

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


Or, yet simpler:

Code
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:


Code
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