ibanix has asked for the wisdom of the Perl Monks concerning the following question:

This is a seperate question from my Matching An IP Address post, but it is related.

I am looking at data of the type:

136.1.1.154:33672 -> 64.210.209.51:80 -> 192.168.1.145:80 + tcp 65.201.211.176:14664 -> 64.210.209.54:80 -> 192.168.1.78:80 + tcp 67.38.95.86:2116 -> 64.210.209.50:80 -> 192.168.1.103:80 + tcp 198.49.222.246:52469 -> 64.210.209.54:80 -> 192.168.1.79:80 + tcp 193.80.106.152:3781 -> 64.210.209.61:80 -> 192.168.1.81:80 + tcp 67.28.79.24:3248 -> 64.210.209.54:80 -> 192.168.1.79:80 + tcp 128.187.192.39:1218 -> 64.210.209.54:80 -> 192.168.1.78:80 + tcp 128.187.192.39:1209 -> 64.210.209.51:80 -> 192.168.1.144:80 + tcp

The regex another user suggested was
my $re_ip = qr/(\d+\.\d+\.\d+\.\d+):\d+/; my $re_line = qr/ ^ \s* $re_ip \s* -> \s* $re_ip \s* -> \s* $re_ip /x;

However, I have found this does not work. It is the $re_line which is the culprit.

The following patterns DO work:
qr/^\s*$re_ip\s*->\s*$re_ip\s*->\s*$re_ip/ qr/^ \s* $re_ip\s*->\s*$re_ip\s*->\s*$re_ip/x qr/^\s*$re_ip\s*-> \s* $re_ip\s*-> \s* $re_ip/x qr/ ^ \s* $re_ip\s*->\s*$re_ip\s*->\s*$re_ip/x

The following do NOT work:
qr/ ^ \s* $re_ip \s*->\s*$re_ip\s*->\s*$re_ip/x qr/ ^ \s* $re_ip\s*->\s*$re_ip \s*->\s*$re_ip/x

Based on this, I think it is the space between a $re_ip and a \s* that is the culprit.

Can anyone explain why? I thought /x was supposed to allow whitespace within a regex.

Thanks.


<-> In general, we find that those who disparage a given operating system, language, or philosophy have never had to use it in pratice. <->

Replies are listed 'Best First'.
Re: Matching IP address continued
by Abigail-II (Bishop) on Nov 20, 2002 at 18:53 UTC
    Really? I don't get any failures:
    #!/usr/bin/perl use strict; use warnings; my $re_ip = qr/(\d+\.\d+\.\d+\.\d+):\d+/; my $re_line = qr/ ^ \s* $re_ip \s* -> \s* $re_ip \s* -> \s* $re_ip /x; my $line_count = my $match_count = 0; while (<DATA>) { $line_count ++; $match_count ++ if /$re_line/ } my $failures = $line_count - $match_count; print "There were $failures failures\n"; __DATA__ 136.1.1.154:33672 -> 64.210.209.51:80 -> 192.168.1.145:80 + tcp 65.201.211.176:14664 -> 64.210.209.54:80 -> 192.168.1.78:80 + tcp 67.38.95.86:2116 -> 64.210.209.50:80 -> 192.168.1.103:80 + tcp 198.49.222.246:52469 -> 64.210.209.54:80 -> 192.168.1.79:80 + tcp 193.80.106.152:3781 -> 64.210.209.61:80 -> 192.168.1.81:80 + tcp 67.28.79.24:3248 -> 64.210.209.54:80 -> 192.168.1.79:80 + tcp 128.187.192.39:1218 -> 64.210.209.54:80 -> 192.168.1.78:80 + tcp 128.187.192.39:1209 -> 64.210.209.51:80 -> 192.168.1.144:80 There were 0 failures

    And I copied and pasted both your examples and your regexes.

    Abigail

Re: Matching IP address continued
by strider corinth (Friar) on Nov 20, 2002 at 18:44 UTC
    I'm guessing that it's because you're mixing /x in $re_line with the plain regexp in $re_ip. This worked fine for me:
    #!/usr/bin/perl while( <DATA> ){ chomp; my $re_ip = qr/(\d+\.\d+\.\d+\.\d+):\d+/x; # added /x here + my $re_line = qr/ ^ \s* $re_ip \s* -> \s* $re_ip \s* -> \s* $re_ip + /x; my @results = /$re_line/; printf "%-18s %-18s %-18s\n", @results; } __DATA__ 136.1.1.154:33672 -> 64.210.209.51:80 -> 192.168.1.145:80 tcp 65.201.211.176:14664 -> 64.210.209.54:80 -> 192.168.1.78:80 + tcp 67.38.95.86:2116 -> 64.210.209.50:80 -> 192.168.1.103:80 + tcp 198.49.222.246:52469 -> 64.210.209.54:80 -> 192.168.1.79:80 + tcp 193.80.106.152:3781 -> 64.210.209.61:80 -> 192.168.1.81:80 + tcp 67.28.79.24:3248 -> 64.210.209.54:80 -> 192.168.1.79:80 +tcp 128.187.192.39:1218 -> 64.210.209.54:80 -> 192.168.1.78:80 +tcp 128.187.192.39:1209 -> 64.210.209.51:80 -> 192.168.1.144:80 +tcp
    --
    Love justice; desire mercy.
      But do you get a failure when you leave out the first /x?

      Abigail

        As it turns out, no. With my tech support days so far behind me, I forgot to assume nothing, even that the problem is real. =) Thanks, Abigail.
        --
        Love justice; desire mercy.
Re: Matching IP address continued
by BrowserUk (Patriarch) on Nov 20, 2002 at 18:46 UTC

    I've done my very best to replicate both your data and your regexs as you supplied them and all the regexes, including the one I gave you appear to match every line of data.

    My results

    C:\test>214537 136.1.1.154:33672 -> 64.210.209.51:80 -> 192.168.1.145:80 tcp Matched! Matched! Matched! Matched! Matched! Matched! Matched! 65.201.211.176:14664 -> 64.210.209.54:80 -> 192.168.1.78:80 tc +p Matched! Matched! Matched! Matched! Matched! Matched! Matched! 67.38.95.86:2116 -> 64.210.209.50:80 -> 192.168.1.103:80 tcp Matched! Matched! Matched! Matched! Matched! Matched! Matched! 198.49.222.246:52469 -> 64.210.209.54:80 -> 192.168.1.79:80 tc +p Matched! Matched! Matched! Matched! Matched! Matched! Matched! 193.80.106.152:3781 -> 64.210.209.61:80 -> 192.168.1.81:80 tcp Matched! Matched! Matched! Matched! Matched! Matched! Matched! 67.28.79.24:3248 -> 64.210.209.54:80 -> 192.168.1.79:80 tcp Matched! Matched! Matched! Matched! Matched! Matched! Matched! 128.187.192.39:1218 -> 64.210.209.54:80 -> 192.168.1.78:80 tcp Matched! Matched! Matched! Matched! Matched! Matched! Matched! 128.187.192.39:1209 -> 64.210.209.51:80 -> 192.168.1.144:80 tc +p Matched! Matched! Matched! Matched! Matched! Matched! Matched! sdasjdlk asd lkajslkdjalks8237472934 ajlksdjaser asdlkjaslkd No match! No match! No match! No match! No match! No match! No match!

    Which leaves me at a loss to explain your findings?

    My test script


    Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
    Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
    Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
    Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

      This file is a direct dump of data I am looking at.

      My snippet of code looks like:

      ... # Setup our IP matching regex my $re_ip = qr/(\d+\.\d+\.\d+\.\d+):\d+/; my $re_test = qr/^\s*$re_ip\s*->\s*$re_ip\s*->\s* $re_ip/x; my $re_line = qr/ ^ \s* $re_ip \s* -> \s* $re_ip \s* -> \s* $re_ip /x; ... # Reset data my @connections = `bigpipe conn dump`; ... foreach my $line (@connections) { print "$line\n" if ($line =~ /$re_test/); $vip{$2}{'conn_count'}++ if ($line =~ /$re_line/); }

      The print statement works as expected; the increment does not. The increment counter works immediately if I change it to if ($line =~ /$re_test/);

      Thanks again for all the help.

      <-> In general, we find that those who disparage a given operating system, language, or philosophy have never had to use it in practice. <->

        Again, running all the regexes, including the $re_test and $re_line from this post (c&p very carefully) against the entire file you linked, the only line that any of them do not match is the first one.

        from vip node proto

        The next question is what version of Perl are you running and on what OS?


        Okay you lot, get your wings on the left, halos on the right. It's one size fits all, and "No!", you can't have a different color.
        Pick up your cloud down the end and "Yes" if you get allocated a grey one they are a bit damp under foot, but someone has to get them.
        Get used to the wings fast cos its an 8 hour day...unless the Govenor calls for a cyclone or hurricane, in which case 16 hour shifts are mandatory.
        Just be grateful that you arrived just as the tornado season finished. Them buggers are real work.

Re: Matching IP address continued
by fruiture (Curate) on Nov 20, 2002 at 18:24 UTC
    I have found this does not work

    What the h... does that mean "it doesn work"? What error/misbehaviour do you get? Am i supposed to guess what's wrong for you?

    But I can answer your question in spite of the lack of information. It's 42.

    update: to make clear the question _is_ insufficient: There are at least two possibilites: The Regexp doesn't compile OR it doesn't match the wanted data. The second possibility can have lots of variants. I am not here to guess all that and definetely not for getting stupid answers. Thanks

    --
    http://fruiture.de
    A reply falls below the community's threshold of quality. You may see it by logging in.