[regex] Regular Expression for any number greater than 0?

I'm looking for a way to check if a number is greater than 0 using regex.

For example:

  • 12 would return true
  • 0 would return false.

This question is related to regex

The answer is


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*$

Ref: https://www.regextester.com/94470


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})?$

Reference: Regex greater than zero with 2 decimal places


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]*$