Regular expression for checking decimal numbers

The regular expression must meet the criteria below. The number of elements before and after the point can be any. Only 1 point is allowed, and a negative sign is allowed only in the first position. I don't need a comma.

Example:

1
-1
-1.
1.
1.2
-.2
-0.2
000.300

All the above expressions must be true.

So if I break ..

  • Optional negative sign in the first place.
  • Zero or more digits before the point.
  • Point is optional. May reach a maximum once. It can also be a pure integer.
  • O or more digits after the dot.

Any help would be appreciated.

+4
source share
3 answers

What you might want is this:

^-?\d*\.?\d*

(-?),
(\d*),
(\.),
(\d*).

, float , @MarcinJuraszek , .

+5

1) :

^:

-:

?:

2)

/d: digit

*: ( )

3)

.:

?:

4) 0

/d: digit

*: ( )

, : ^ -?/d *.?/d *

+2

Here is my solution

Pros

  • User can specify accuracy
  • Take a comma or period
  • Accept negative values
  • Accept integers

vs

  • not working for .0 or 0 .: (

    [-] \ d {1,18}? (?: [.,] \ D {1,2})? $

0
source

All Articles