in reply to Is this a bug in perl regex engine or in my brain?

I'd be inclined to do it like this:
while (<>) { chomp; my $result = ( /^([1-9]\d*)$/ and $1 < 256 ) ? "matched" : "did no +t match"; $_ .= " $result\n"; print; }
That rejects anything with a leading zero (hence "0"), and anything greater that 255.

Replies are listed 'Best First'.
Re^2: Is this a bug in perl regex engine or in my brain? (matching from 0 to 255)
by Discipulus (Canon) on Oct 07, 2015 at 07:28 UTC
    I very like this approach. It saves us from the "I had aproblem, i used a regex, now i have two problems" situation.
    If you (the OP) want a review of most possible implications concerning the parsing of an IP address (numbers from 0 to 255 are an octet of an IP address expressed in the dotted decimal form) you can read some paragraph of "Mastering regular expressions" where the proposed result is:
    [01]?\d\d?|2[0-4]\d|25[0-5]
    on the same argument you can find interesting an old thread: Don't Use Regular Expressions To Parse IP Addresses! or some exaples on another site.

    Regex::Common::net is used for the exact purpose and if you feel strong you can dive in it's source code to see how to match an octet.

    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.