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

Re: Is this a bug in perl regex engine or in my brain?

by graff (Chancellor)
on Oct 07, 2015 at 02:16 UTC ( [id://1144011]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1144011]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-19 07:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found