RegEx - exclude zero

How can I exclude input 0 in this regex? The user should be able to enter any number ... but not zero

^([0-9]*|\d*\.\d{1}?\d*)$

Thank.

0
source share
6 answers

Next regex

^(?!0*(\.0+)?$)(\d+|\d*\.\d+)$

Does not match

0
0.0
 .0

But allows

  .1
 0.01
 1.00
 1.1
01.01
10.10
 1
+6
source

If I understand your requirement, use [1-9]instead of [0-9]or \d:

^([1-9]*|[1-9]*\.[1-9]{1}?[1-9]*)$
0
source

: ^ ([1-9] +).\d + $ 1 1-9

^ ([1-9]).\d $ , -, , , 1-9

0

:  * [0-9] [1-9]  */d [1-9]

: ^([0-9]*|\d*\.\d{1}?\d*)$

: ^([1-9]*|[1-9]*\.[1-9]{1}?[1-9]*)$

\ d - [0-9]

0
source

How about this:

^((?:[1-9][0-9]*)(?:\.[0-9]+)?)$

Match:

1
5
10
22
5000
1.0
10.10
123.456

No match:

0
00
007
0.0
0.000
0.50
0.01
5,432.10
1,234,567
10.

You can block it to certain ranges, as described here:

http://www.regular-expressions.info/numericranges.html

0
source

This only works with natural numbers up to 5 digits and limits five digits to 0 on the left (you can personalize this limit)

/^[0]{0,5}[1-9]\d{0,4}$/

match:

1
01 .. 000001
0100000 .. 0000010000

does not match:

0
00 .. 00000
0
source

All Articles