Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex to allow "single 0" or " any other number of digits?

Tags:

regex

I need a regex to allow either a single 0 or any other number of digits that do not start with zero so:

0 or 23443 or 984756 are allowed but 0123 is not allowed.

I have got the following which allows only 1 to 9

[1-9]\d
like image 681
Farhad-Taran Avatar asked Sep 01 '25 02:09

Farhad-Taran


1 Answers

Look for a lone 0 or 1-9 followed by any other digits.

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

If you want to match numbers inside of larger strings, use the word boundary marker \b in place of ^ and $:

\b(0|[1-9]\d*)\b
like image 105
John Kugelman Avatar answered Sep 10 '25 04:09

John Kugelman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!