in reply to regex in perl

The safest way to match an ipv6 address is to use the module Regexp::Common::net. Use a second regex to select the ones you want. I do not exactly understand your requirements, but this should get you started.
use Regexp::Common qw /net/; ...; my @temp = grep {$RE{net}{IPv6}{-sep=>':'}{-style=>'HeX'}} @$records; my @ip = grep {!/^[a-f0-9]{4}\:[a-f0-9]{4}\:ffff/i} @temp;

UPDATE:

The original syntax is correct, but AnomalousMonks' suggestion is probably clearer.

Soonix's requirement can be met by specifying a regex for sep. It probably will not extract the ipv4 address correctly, but that is not a requirement in this case.

{-sep=>qr/[:.]/}
Bill

Replies are listed 'Best First'.
Re^2: regex in perl
by AnomalousMonk (Archbishop) on Jun 15, 2016 at 06:47 UTC
    my @temp = grep {$RE{net}{IPv6}{-sep=>':'}{-style=>'HeX'}} @$records;

    I haven't tested it, but I think the  $RE{net}{IPv6}{-sep=>':'}{-style=>'HeX'} expression is just a Regexp object and needs, in this case, to be interpolated into an  m// operator:
        my @temp = grep { m/$RE{net}{IPv6}{-sep=>':'}{-style=>'HeX'}/ } @$records;
    Or slightly more simply:
        my @temp = grep m/$RE{net}{IPv6}{-sep=>':'}{-style=>'HeX'}/, @$records;
    In the second case, note the  m// is delimited by a  ,(comma).

    Update: Or you could explicitly bind to the "topicalization" scalar  $_ with the  ~= operator (again, untested):
        my @temp = grep { $_ =~ $RE{net}{IPv6}{-sep=>':'}{-style=>'HeX'} } @$records;
    or:
        my @temp = grep $_ =~ $RE{net}{IPv6}{-sep=>':'}{-style=>'HeX'}, @$records;


    Give a man a fish:  <%-{-{-{-<

Re^2: regex in perl
by AnomalousMonk (Archbishop) on Jun 16, 2016 at 04:37 UTC
    The original syntax is correct ...

    The original syntax may be correct in that it compiles without error, but it is semantically incorrect in that it doesn't actually do anything. grep looks at the truthiness of the evaluated expression or statement block, and a Regexp object alone is always true. Only when evaluated within a match operator can it result in a true or false value. Consider:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use Regexp::Common qw /net/; ;; my $records = [ qw(1.2.3.4 12.23.34.45 9.9.9 x.x.x.x anything) ]; ;; my @temp = grep {$RE{net}{IPv4}} @$records; dd \@temp; ;; @temp = grep { m/$RE{net}{IPv4}/ } @$records; dd \@temp; " ["1.2.3.4", "12.23.34.45", "9.9.9", "x.x.x.x", "anything"] ["1.2.3.4", "12.23.34.45"]

    Update: The code would also have been semantically correct had it used an explicit binding of  $_ to the Regexp object:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "use Regexp::Common qw /net/; ;; my $records = [ qw(1.2.3.4 12.23.34.45 9.9.9 x.x.x.x anything) ]; ;; my @temp = grep { $_ =~ $RE{net}{IPv4} } @$records; dd \@temp; " ["1.2.3.4", "12.23.34.45"]


    Give a man a fish:  <%-{-{-{-<

      Thanks for the explanation! I am must check for the analogous error in other projects.
      Bill
Re^2: regex in perl
by soonix (Chancellor) on Jun 15, 2016 at 09:21 UTC
    My guess (from looking at the source) is, however, that Regexp::Common::net doesn't (yet?) know about the mixed "alternate form" with colons and dots.