
Laurent_R
Veteran
/ Moderator
Mar 9, 2013, 3:53 PM
Post #9 of 10
(7030 views)
|
Re: [Kenosis] Regex source -> destination line matches...
[In reply to]
|
Can't Post
|
|
And you could even: ( $line =~ /($ip)/g )[ 1, 2 ] I tried that, or something very similar to that, when I wrote my previous message, but it did not seem to work properly. I must have made a silly mistake somewhere when I tested it. Anyway, what really seemed to puzzle the OP was how to pick up the second and third IP addresses and not the first one, so my examples really focussed on various ways to do that, and were hopefull progressively built for pedagogical purposes.
You made an excellent point: IP regex should be made much more selective... And this is why I like using Regexp::Common for such a job. I fully agree. My very simplistic IP regex was just for the purpose of demonstrating the code that followed, and I said it was too simplistic. At the very least, I would check for the number of digits in each segment with something like this:
my $ip = qr/(?:\d{1,3}\.){3}\d{1,3}/; or, better, I would be trying to check at least to a certain extent the ranges of each octet one way or another. But, then, of course, there may be a mistake in my code above or in whichever code I would try to add for checking the range (and I know for a fact it is not an easy task to get it really right), it is unlikely that I will have tested it nearly as thoroughly as the Regexp::Common module has been tested. So, yes, agreed, using such a module is usually a better idea. Having said that, the files I am working on are usually not free format files, I can usually get away with quite simpler validation rules. But I also have had to try to validate URLs or e-mail addresses (or, I should rather say, I had to to try to detect improper URLs or e-mail addresses), that can easily become a real nightmare.
|