Repeated floating number from 0 to 1

I am trying to validate input for a floating number which has a maximum value 1.0

and a minimum value 0

.

Min : 0
Max : 1

      

Possible values;

0.1
0.99
0.365

      

how can i succeed with regex?

+4


source to share


9 replies


As a javascript regex literal:



 /^(0(\.\d+)?|1(\.0+)?)$/

      

+6


source


0(\.\d+)?|1\.0

      

Explanation:

0            # a zero
(\.\d+)?     # a dot and min 1 numeric digit - this is made optional by ?
|            # or
1\.0         # one, a dot and a zero

      



If you need this to match the entire sring, you need the carriage and dollar signs that represent the start of the line and the end of the line, respectively, as in ^(0(\.\d+)?|1\.0)$

Also, if you want to find possible negative numbers, you will need to add an optional minus sign, as in ^-?(0(\.\d+)?|1\.0)$

. For exhibitors, of course, you need to change the template.

+5


source


This RegEx should only do well:

/((0(\.[0-9]*)?)|(1(\.0)?))/

      

Unless you plan on matching exponential floating points.

+2


source


This worked for me:

0+([.][0-9]+)?|1([.]0)?

      

Or am I missing anything? :)

+2


source


What is it:

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

      

+2


source


This will match floats with an optional [0,1] sign, but will not match if scientific notation is used or the number starts with a decimal point.

\+?(0(\.[0-9]+)?|1(\.0+)?)

      

+1


source


Common decision:

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

Formatted:

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

      

+1


source


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

This will accept numbers [0,1] in the following formats

0 0.5 .5 1 1.0 1.000

0


source


Just read the brief information about regular expressions. Hope it works!

^(0.0*[1-9](\d+)?)$

      

Let me know if I am missing something.

0


source







All Articles