-3

I need to find a regular expression that will only allow input between 1.00 and 99.00 So anything below 1.00 and greater than 99.00 is not acceptable.

  • 1
    Duplicate: [https://stackoverflow.com/questions/26958458/can-we-validate-min-and-max-value-for-a-floating-number-using-regexp](https://stackoverflow.com/questions/26958458/can-we-validate-min-and-max-value-for-a-floating-number-using-regexp) – Schlumpf Jun 23 '17 at 15:11
  • 2
    `regex` is not the appropriate tool for the job. You can use `regex` to check if the string looks like a number then extract the numeric value from it and compare that value against the limits of the acceptable range. – axiac Jun 23 '17 at 15:13
  • So anything below 1.00 and greater than 90.00 is not acceptable. Don't you mean 99.00 in that second sentence, or 90.00 in that first sentence? – Gordon Bell Jun 23 '17 at 15:14
  • 1
    How on earth this question received 2 upvotes?! – Thomas Ayoub Jun 23 '17 at 15:32
  • Don't. Just don't. What's the problem with `double n; if (double.TryParse("99.0", out n) && n >= 1.0 && n <= 99.0) Console.WriteLine("Valid.");` No one will be able to understand and maintain that Regex. What will you do when the valid range has to be changed? Come back to StackOverflow and ask the same question again? – Good Night Nerd Pride Jun 23 '17 at 16:01

3 Answers3

0

Try this- This will match 1 to 90

 ^[1-9][0-9]?$|^90$

Working Fiddle

ISHIDA
  • 4,700
  • 2
  • 16
  • 30
0

If you need to accept integers too:

^(?(?=99)99(\.0+)?|([1-9]\d?(\.\d+)?))$

Live demo

revo
  • 47,783
  • 14
  • 74
  • 117
0

Regex is bad for this kind of validation because there are too many if this then that rules involved. This pattern will work, though.

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

It has been tested for all 10,000 possible values between 0.00 and 99.99

Nick
  • 4,556
  • 3
  • 29
  • 53
  • Doesn't work between 99.01 and 99.99 because you're setting 99.00 literally. Pretty close though! Thanks. – wheeleruniverse Feb 27 '20 at 19:20
  • (^\d{1}\.|[1-9]\d\.)(\d{2})$ – wheeleruniverse Feb 27 '20 at 19:30
  • @jDub9 The question says valid range is only up to 99.00, and that is why I provided an answer that only matches up to 99.00. If you have different validation rules than what the question is asking for, that's fine, but you can't tell me that I answered this question incorrectly just because you have a different question. – Nick Mar 02 '20 at 20:33