I'm looking for a way to check if a number is greater than 0 using regex.
For example:
12
would return true0
would return false.This question is related to
regex
there you go:
MatchCollection myMatches = Regex.Matches(yourstring, @"[1-9][0-9]*");
on submit:
if(myMatches.Count > 0)
{
//do whatever you want
}
You can use the below expression:
(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\.?\d*$)
Valid entries: 1 1. 1.1 1.0 all positive real numbers
Invalid entries: all negative real numbers and 0 and 0.0
What about this: ^[1-9][0-9]*$
Simplified only for 2 decimal places.
^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$
I think this would perfectly work :
([1-9][0-9]*(\.[0-9]*[1-9])?|0\.[0-9]*[1-9])
Valid:
1
1.2
1.02
0.1
0.02
Not valid :
0
01
01.2
1.10
The simple answer is: ^[1-9][0-9]*$
[1-9]\.\d{1,2}|0\.((0?[1-9])|([1-9]0?)){1,2}\b
Another solution:
^[1-9]\d*$
\d
equivalent to [0-9]
I Tried this one and it worked for me for all decimal/integer numbers greater than zero
Allows white space: ^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$
No white space: ^(?=.*[1-9])\d*(?:\.\d{1,2})?$
Very simple answer to this use this: \d*
If you only want non-negative integers, try:
^\d+$
I think the best solution is to add the + sign between the two brackets of regex expression:
^[1-9]+[0-9]*$
Source: Stackoverflow.com