Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333] : superdoc . print w/replies, xml ) Need Help??

Inspired by Regexp to match IP address, I thought of golfing the smallest Perl expression (not necessarily a regular expression) that, when using strict and warnings, evaluates to true if $_ is a valid numeric IPv4 decimal address.

Those addresses can be:

    A single decimal number between 0 and 256 ** 4 - 1
    Two decimal numbers separated by a dot, the first between 0 and 255 and the second between 0 and 256 ** 3 - 1
    Three decimal numbers separated by a dot, the first two between 0 and 255 and the third between 0 and 65535
    The more usual four decimal numbers between 0 and 255 separated by a dot

Here is the first naive attempt to get the ball rolling. It's 24 + 39 + 54 + 64 = 181 characters, easy to beat!

Update: It looks like I didn't make myself clear, when I said "smallest expression" I meant "smallest valid Perl expression", I didn't mean "regular expression"

# 1 2 3 4 5 6 #234567890123456789012345678901234567890123456789012345678901234 (/^(\d+)$/&&$1<256**4)|| (/^(\d+)\.(\d+)$/&&$1<256&&$2<256**3)|| (/^(\d+)\.(\d+)\.(\d+)$/&&$1<256&&$2<256&&$3<256**2)|| (/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/&&$1<256&&$2<256&&$3<256&&$4<256)

In reply to (Golf) Expression for matching numeric IP addresses by trantor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.