Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

RE: Matching an IP address

by young perlhopper (Scribe)
on Aug 02, 2000 at 00:55 UTC ( #25592=note: print w/replies, xml ) Need Help??

in reply to Matching an IP address

Are you sure you want to allow 0 as the first character? Or three 0's as the first octet? IP addresses don't really lend themselves to regular expressions. If you really want to check for correctness i'd do something like this:

foreach $octet (split /\./, $ipaddr) { # # check each octet here (numerically, not with # a regex!), i've gotta jet # or i'd write it myself # }
Okay, okay, it ain't briefer, but it will probably enforce something more like an actual IP address.

Then again, if you are just trying to pick out IP addresses in text, you might be alright with your regex... You won't find strings like '304.00.3.999' very often in text files.

Hope this helps,

Replies are listed 'Best First'.
RE: RE: Matching an IP address
by Fastolfe (Vicar) on Aug 02, 2000 at 00:57 UTC
    Why exclude
      I didn't explicitly say that you should... i was just pointing out that his regex looks like it would allow:
      etc, etc...

      Thanks for pointing that out though, i wasn't terribly clear... -Mark

        This is a good point, and I believe some stacks even treat octets with a leading zero as octal versions of the IP address (!), in which case none of the solutions I've seen presented on this topic would be adequate.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2022-08-09 05:13 GMT
Find Nodes?
    Voting Booth?

    No recent polls found