Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

regexp::common::net unexpected failure

by neilwatson (Priest)
on Nov 24, 2016 at 20:11 UTC ( [id://1176510]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings,

Why does the last test fail?

#!/usr/bin/perl use Regexp::Common qw/ net number /; use Test::More; use strict; use warnings; my $valid_host = qr/\A (?: $RE{net}{IPv6} ) | (?: $RE{net}{IPv4} ) | (?: $RE{net}{domain}{-nospace}{-rfc1101}\.? ) \Z/x; like('luna.example.com', $valid_host, 'valid long hostname' ); like('luna', $valid_host, 'valid short hostname' ); like('fe80::42:5eff:feef:8f4f', $valid_host, 'valid ipv6 ip' ); like('10.0.0.1', $valid_host, 'valid ipv4 ip' ); unlike('10.0 24', $valid_host, 'invalid host or ip' ); done_testing; ok 1 - valid long hostname ok 2 - valid short hostname ok 3 - valid ipv6 ip ok 4 - valid ipv4 ip not ok 5 - invalid hostname or ip # Failed test 'invalid hostname or ip' # at /home/neil/src/neil/test/ipv4.pl line 22. # '10.0 24' # matches '(?^x:\A # .... # # \Z)' 1..5 # Looks like you failed 1 test of 5.

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: regexp::common::net unexpected failure
by Corion (Patriarch) on Nov 24, 2016 at 20:41 UTC

    The problem is that \A only applies to the first alternation and \Z only to the last. You want parentheses to make the anchors relevant to all alternations:

    my $valid_host = qr/\A (?: (?: $RE{net}{IPv6} ) | (?: $RE{net}{IPv4} ) | (?: $RE{net}{domain}{-nospace}{-rfc1101}\.? ) ) \Z/x;
Re: regexp::common::net unexpected failure
by talexb (Chancellor) on Nov 24, 2016 at 20:22 UTC

    Because '10.0 24' isn't a valid host?

    Alex / talexb / Toronto

    Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

      The test is unlike. Doesn't that mean that the string and the regex should not match?

      Neil Watson
      watson-wilson.ca

        Yes -- my bad. :(

        Alex / talexb / Toronto

        Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1176510]
Approved by talexb
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-28 23:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found