0

I'm trying to create a regex string that allows values 0.0 - 5.0. I need the one decimal point to be required. The string below gets me there, but it also allows 5.1-5.9. How do I prevent 5.1-5.9 from being entered, and allow 5.0?

^[0-5]+(\.[0-9]{1})$
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • 2
    Checking numeric values with regexes is probably not a good idea. Why are you trying to do it with regexes? What language are you working with? – Andy Lester Sep 18 '17 at 21:44
  • I'm going to say, regardless of what language you use, *don't* do this with regex. It'd be nice if you clarified what language you *were* using, but this is one of those nice situations where a regex is actually a poor choice. – Makoto Sep 18 '17 at 21:48
  • 1
    @WiktorStribiżew your regex can match 44.9 which is greater than 5.0, Take a look at my answer :) – Youssef13 Sep 18 '17 at 21:49
  • I'm using an application that allows you to define an answer to a question using regex. I'm not programming in any language, it's just a text field with regex support. Thanks for the help everyone! – Kade Jansson Sep 18 '17 at 21:55

1 Answers1

1

Try this regex:

^([0-4]\.[0-9]|5\.0)$

It matches any number from 0 to 4 then dot then any number. it also matches 5.0

Note: Your regex has another problem that you used + after [0-5] which also matches 55 for example, so you need to remove the +. You also need to remove {1}, It won't make any change but it's useless.

Youssef13
  • 3,836
  • 3
  • 24
  • 41