Thread: Match 128-255

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    1
    Rep Power
    0

    Match 128-255


    I'm trying to match the last octet of an IP address with addresses ending in range 128-255.

    I thought this should work:

    ^[0-9]*\.[0-9]*\.[0-9]*\.[128-255]

    I've been googleing and trying out different expressions for the past few hours. I can't seem to get this one.

    Thanks!
  2. #2
  3. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,753
    Rep Power
    9397
    Character sets work with characters. They have no concept of number ranges: "128-255" means "characters 1, 2, 8 through 2, 5, and another 5".

    It's complicated so enjoy:
    Code:
    (12[89]|1[3-9]\d|2[0-4]\d|25[0-5])
    It would be easier if you simply matched captured that octet and checked in code if is was in your desired range.

    Comments on this post

    • ManiacDan agrees
    Last edited by requinix; August 13th, 2012 at 03:49 PM.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,923
    Rep Power
    1045
    Hi,

    The character class [...] matches a single character inside a certain group. You cannot use it to describe complex expressions.

    A regular expression is generally the wrong tool for number checking, because a simple range like "128-255" is pretty difficult to describe on syntax level.

    It would be something like

    12[89]|1[3-9][0-9]|2[0-4][0-9]|25[0-5]

    So you should rather do that in your application.

    Comments on this post

    • ManiacDan agrees
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,595
    Rep Power
    4207
    Matching IP addresses with regexs is a very bad idea and is best left to masochists. There are some good reasons to use regular expressions, but this isn't one of them.

    Best thing to do would be to split the IP address into four octets using . as the delimiter. Then simply convert the last octet to int and check if it is within the range you expect.

    See this discussion on Raymond Chen's blog for details.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,923
    Rep Power
    1045
    I think this has been said often enough now...

IMN logo majestic logo threadwatch logo seochat tools logo