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

Grettings. Why does this test fail?

#!/usr/bin/perl use strict; use warnings; use Test::More; use Regexp::Common qw/ net /; my $ip_query = "2 001:470:1d:a2f::%"; my $regex = qr/\A (?: $RE{net}{IPv6} ) | # A full IPv6 address (?: [0-9a-f:%]+ ) # A portion of an IPv6 address with SQL % wi +ldcards \Z/imsx; unlike( $ip_query, $regex, "Test [$ip_query] against big regex" ); done_testing();

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Roughly matching an ipv6 address with sql wildcard
by graff (Chancellor) on Sep 10, 2015 at 01:18 UTC
    I'm not sure I understand the question. Are you asking: "Because there is a space in the value of $ip_query, it should not match $regex, but the test shows that it does match. Why is that?"

    If so, it's interesting that if I take (?: $RE{net}{IPv6} ) | out of the code, it behaves as expected -- that is, the value of $ip_query does not match $regex, when $regex is just (?: [0-9a-f:%]+)

    Also interesting: when $regex is just (?: $RE{net}{IPv6} ) the string does not match, as expected -- that is, the test passes.

    Looks like you need a set of parens that surrounds the conjunction alternation:

    my $regex = qr/\A ( (?: $RE{net}{IPv6} ) | # A full IPv6 address (?: [0-9a-f:%]+ ) # A portion of an IPv6 address with SQL % wi +ldcards ) \Z/imsx;

      You are right on all counts. I do not know why the extra brackets are needed. I must sleep on it.

      Neil Watson
      watson-wilson.ca

        The extra parens are needed in order for the anchors to do what you want. The OP regex was really saying: "if the string STARTS with an IPv6 string, OR if it ENDS with something that contains one or more IP address characters and/or percent signs".

        BTW, you might want to include underscore as a possible sql wildcard character, because it is (and can be very useful in certain cases).