Thread: Another Regex

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

    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0

    Another Regex


    My brain is starting to get into it, but not quite.

    I want to check for a float number, which would accept all of the following:

    1
    1.1
    .1
    0 through 999
    remove spaces surrounding
    return the entire number

    it should not accept:
    .
    letters, etc...

    Here's my expression so far:

    ^\s*((?:[0-9]{1,3}(?:\.[0-9]*)?)|(?:.[0-9]+))\s*$

    Here's what I THINK it does--please correct me (since I'm obviously wrong):

    EITHER
    3 digits, optional decimal by unlimited digits
    OR
    decimal, unlimited digits

    but it accepts 131313. I don't understand why. It shouldn't accept anything larger than 3 digits (999).

    Help is appreciated.

    Jay
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2008
    Posts
    14
    Rep Power
    0
    I started with an example from RegexBuddy and fashioned it into this:

    /^\s*([0-9]{0,3}(?:\.[0-9]*)?)\s*$/

    I then checked outside that that the value wasn't null (which should cause nothing to happen).

    I guess I'm getting it a bit more...

IMN logo majestic logo threadwatch logo seochat tools logo