Regex Pattern for Numeric Values [Closed]

Regex Pattern for Numeric Values [Closed]

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?

1

5 Answers

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

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

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

2

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
/([1-9][0-9]*)|0/
0
/^0|[1-9]\d*$/
Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.