[regex] Regex pattern for numeric values

I need an regular expression pattern to only accept positive whole numbers. It can also accept a single zero.

I do not want to accept decimals, negative number and numbers with leading zeros.

Any suggestions?

This question is related to regex

The answer is


This will allow decimal numbers (or whole numbers) that don't start with zero:

^(([1-9]*)|(([1-9]*)\.([0-9]*)))$

If you want to allow numbers that start with zero, you can do :

^(([0-9]*)|(([0-9]*)\.([0-9]*)))$

"[1-9][0-9]*|0"

I'd just use "[0-9]+" to represent positive whole numbers.


"[1-9][0-9]*|0"

I'd just use "[0-9]+" to represent positive whole numbers.


This will allow decimal numbers (or whole numbers) that don't start with zero:

^(([1-9]*)|(([1-9]*)\.([0-9]*)))$

If you want to allow numbers that start with zero, you can do :

^(([0-9]*)|(([0-9]*)\.([0-9]*)))$

/^0|[1-9]\d*$/

"[1-9][0-9]*|0"

I'd just use "[0-9]+" to represent positive whole numbers.


/([1-9][0-9]*)|0/

/^0|[1-9]\d*$/

/([1-9][0-9]*)|0/

/([1-9][0-9]*)|0/

/^0|[1-9]\d*$/