3

I am trying to exclude all decimal numbers below 200. For example:

50.123 invalid
150.103 invalid
205.111 valid

With the ([0-9][0-9])|(1[0-9][0-9])\.* reg expression, I am getting the following results:

50.123 valid but should be invalid
150.103 invalid
205.111 valid

Can anyone tell me why any decimal number < 100 is valid?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
darego101
  • 319
  • 2
  • 15
  • 200.* is currently invalid with the above regex, which is correct. the problem is anything below 100 is also invalid when it should be valid – darego101 Feb 10 '21 at 16:19
  • 4
    You may use: `^[01]?[0-9]{1,2}(?:\.[0-9]+)?$` – anubhava Feb 10 '21 at 16:21
  • 2
    Your regex has a bug: it ends with `\.*` but should end with `\..*` or more precisely `\.\d*`. Your regex requires input to end with any number of dots, rather than a dot then any number of chars/digits. – Bohemian Feb 10 '21 at 16:25
  • @anubhava I edited my initial post to try an make it more easy to understand. thank you for the replies! – darego101 Feb 10 '21 at 17:06
  • Are you trying to validate numbers between `100` and `200` only? – anubhava Feb 10 '21 at 17:08
  • @anubhava I am trying to exclude all numbers below 200 (e.g. 0-199 should be invalid). Although, my first regex was showing 0-99 as valid and 100-199 as invalid, when every number below 200 should be invalid. Hope that clears up the confusion – darego101 Feb 10 '21 at 17:10
  • 3
    @anubhava yes your regex works perfecly for me, thank you for your help – darego101 Feb 10 '21 at 17:19
  • 5
    People voting on removal, [please read this meta post](https://meta.stackexchange.com/questions/10841/how-should-duplicate-questions-be-handled/10844#10844). **Should duplicates be deleted?** In general, no: most duplicates stay around. Having multiple copies of the same question with different wording is useful as search fodder, because people looking for an answer may use different wording too. – anubhava Feb 23 '21 at 08:16
  • 3
    Does this answer your question? [Regex for age validation that accepts an age between 0-200 using Javascript only](https://stackoverflow.com/questions/29467075/regex-for-age-validation-that-accepts-an-age-between-0-200-using-javascript-only) – oguz ismail Feb 27 '21 at 14:40
  • 2
    That is not the right dupe as OP is also trying to validate digits after decimal point here and this question is not tagged as Javascript. – anubhava Mar 18 '21 at 06:57

4 Answers4

8

You may use this regex to validate all your numbers below 200:

^[01]?[0-9]{1,2}(?:\.[0-9]+)?$

RegEx Demo

RegEx Details:

  • ^: Start
  • [01]?: Match 0 or 1 or nothing
  • [0-9]{1,2}: Match 1 or 2 digits
  • (?:\.[0-9]+)?: Optionally match . followed by 1+ digits
  • $: End
anubhava
  • 761,203
  • 64
  • 569
  • 643
5

Looking at the fact you want to exclude numbers below 200 and your sample data that showed invalid examples, I think you would need:

^(?:[2-9]\d{2,}|1\d{3,})(?:\.\d+)?$

See the online demo

  • ^ - Start string anchor.
  • (?: - 1st Non-capture group:
    • [2-9]\d{2,} - Match number ranging from 2-9 followed 2+ digits.
    • | - Or:
    • 1\d{3,} - A 1 followed by at 3+ digits.
    • ) - Close non-capture group.
  • (?: - Open 2nd non-capture group:
    • \.\d+ - A literal dot followed by 1+ digits.
    • )? - Close 2nd non-capture group and make it optional.
  • $ - End string anchor.
JvdV
  • 70,606
  • 8
  • 39
  • 70
4
/(?<!\d)
(\d{2}|1\d{2})\.\d+/g

\d{2}: 2 digits

(?<!\d): not have a digit before it => avoid 05.111 in 205.111 being matched

I tried your regex but it has problems,so I came up with another solution.

Dharman
  • 30,962
  • 25
  • 85
  • 135
3

Thank you for the answers (which all work correctly).

Bohemian was also correct in saying that:

it ends with \.* but should end with \..* or more precisely \.\d*

So I edited my regular expression to the following which now works fine:

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

darego101
  • 319
  • 2
  • 15