Regular expression number greater than 0


  • Hi,

    I need a regular expression to check if a number(decimal) is greater than 0.  Which means the following should pass

    And the following should fail
    1 a

    I tried a number of expressions but they all fail for either one of the scenarios.  Does anyone know of any expression that I can use.

    Wednesday, April 09, 2008 5:20 AM

All replies

  • I don't believe you can implement that kind of business logic with regex (# > x), but if you can describe your rule in terms of characters, numbers, upper/lower case, digits, alpha numerics, then you're all set.  According to your strings that should fail, you could easily validate using a regex that only accepts numbers and fails when there is anything alpha in the string.  As for the > 0, you'll need to do that check in code.


    You could be looking for something like \d+.  The \d looks for digits only and the period says you have to have "one or more" of the things before it (digits).  You could use a star (*) but that star means zero or more digits, if you want to have "" as acceptable input then use the *.  If not, if you want to have at least one digist, use the plus (+).


    Regex is not a simple thing, if you're fighting with it, then that means you're learning (and progressing forward :>).  I would suggest getting a tool like Expresso to help you with validating your regex and input strings.


    Wednesday, April 09, 2008 2:14 PM
  •  Arjuna Indrajith Marambe wrote:

    I need a regular expression to check if a number(decimal) is greater than 0.  Which means the following should pass


    For the lack of a better solution how about this simple idea:


    Regex reg = new Regex ( "(\b[\d\.]*)" );

    MatchCollection col =reg.Matches ( digit );

    string result = col[0].Value;


    if (digit != result)


    // negative number is thrown out.




    // do something since it is positive.



    It seems all your cases that are supposed to fail did fail this test. Also this regex strips the negation sign from up front, leaving just digits and a decimal point.

    Wednesday, April 09, 2008 7:38 PM
  • Hi

    I think this will do the job "^(([1-9]+([.][0-9]+)?)|(0[.][0-9]*[1-9]))$"

    Thursday, April 10, 2008 12:55 PM
  • This will work for all of your test cases: [0-9]*\.?[0-9]*[1-9]
    Thursday, April 10, 2008 8:33 PM
  • Hi,


    These are my findings


    ^(([1-9]+([.][0-9]+)?)|(0[.][0-9]*[1-9]))$ Fails when there is a 0 anywhere.  For example 01 or 101 will fail, but I want it to pass this.

    [0-9]*\.?[0-9]*[1-9]  passes for string like '1 a' even whereas this should fail.


    Does anyone have any other ideas or suggestions.





    Wednesday, April 16, 2008 10:37 AM
  • I think this will do it.


    Code Snippet




    The trick is to check the entire string first.


    1) If the whole part contains at least one digit between 1 and 9


    2) If the fractional part contains at least one digit between 1 and 9


    Then - match.



    Wednesday, April 16, 2008 11:44 AM
  • Hi

    Now it should work "^(([0-9]*[1-9][0-9]*([.][0-9]+)?)|([0]+[.][0-9]*[1-9][0-9]*))$".

    Wednesday, April 16, 2008 11:53 AM
  • Very good.  But one more modification makes it even shorter:


    Code Snippet




    Or if you prefer to enforce the leading 0 and trailing digits if a decimal exists:


    Code Snippet




    Question is ... can it be made even shorter than that?


    I'm guessing that any variations at this point are just cosmetic.


    Excellent contributions one and all.



    • Proposed as answer by James Alvarez Monday, March 01, 2010 3:36 AM
    Thursday, April 17, 2008 9:17 AM