http://qs321.pair.com?node_id=25592


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,
Mark

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

      023.45.34.2
      000.34.23.2
      etc, etc...

      Thanks for pointing that out though, i wasn't terribly clear... -Mark mlogan@ccs.neu.edu

        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.