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

    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0

    Allowing a decimal OR whole number


    Hi,

    I have the following RegEx and cant figure out how to adapt it to suit my needs.
    Currently it will allow a number between 1-100

    However I want it to accept a number between 1-100 either as a whole number or a decimal.

    Current RegEx

    Code:
    var discountRegEx = /^([a-zA-Z]+\d?)-(5[0-2]|[1-4]\d|[1-9])-(100|[1-9]\d|[1-9])$/i;
    The code in bold is what I need to adapt..

    Please any advice would be much appreciated.

    Thanks in advance,

    Joe
  2. #2
  3. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    How about (untested):

    Code:
    (100|[1-9]\d?(?:\.\d+)?)
    That won't allow 100.0 though: do you want that too.

    Essentially the second part is:
    [1-9] - a character between 1 and 9
    \d? - optionally followed by another digit
    (?: - start a non-capturing group
    \.\d+ - a decimal point followed by one or more digits
    ) ?- close the non-capturing group and make matching its contents optional
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0
    Originally Posted by ishnid
    How about (untested):

    Code:
    (100|[1-9]\d?(?:\.\d+)?)
    That won't allow 100.0 though: do you want that too.

    Essentially the second part is:
    [1-9] - a character between 1 and 9
    \d? - optionally followed by another digit
    (?: - start a non-capturing group
    \.\d+ - a decimal point followed by one or more digits
    ) ?- close the non-capturing group and make matching its contents optional
    Thats perfect you are a legend! I find ReqEx so difficult so this is much appreciated!

    I adapted it so that I could enter 0. something

    (100|[0-9]\d?(?:\.\d+)?)

  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2010
    Posts
    8
    Rep Power
    0
    Can I ask what you do for a living?

    Just wondering if there are jobs specific to RegEx or are you a programmer of some sort. If so which language(s)

    Cheers
  8. #5
  9. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    Originally Posted by JoeSad
    Can I ask what you do for a living?
    You can! I'm an academic, pursuing a PhD.
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    There is no reason to use regular expressions for this at all, except as a thought exercise. Convert the input to a number, and then check if it's greater than zero and less than 100.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo