Regular expression for a range of numbers from 0.5 to infinity

In regular expressions, there are no ready-made templates for ranges of numbers, especially for fractional ones. Because regulars are primarily text processing. Regulars are able to work only with individual digits, not numbers. But numbers are a text consisting of numbers written in a certain pattern. That is, you need to separate the numbers — these are signs 0-9 and numbers — compound "words" from these signs.

Divide into subtasks

Let's think about the task. Our big task actually consists of two smaller ones:

  1. Numbers from 0.5 inclusive to 1, not including it in the range.
  2. Numbers from 1 to ∞ inclusive.

Let's write regular expressions for each case separately.

Numbers from 0.5 and more, but less than 1

Let's try to write down in words what we want to get.

Numbers in the range [0.5, 1):

  1. Be sure to start with the digit 0.
  2. There must be a dot or comma after zero.
  3. There must be a number from 5 to 9 inclusive.
  4. Optionally, there may be additional digits after.

^0[.,][5-9]\ d{0,}?$ — it turned out to be such a regular:

  1. ^ — the beginning of the line (not to look for values in any part of the text).
  2. 0 is actually zero.
  3. [.,] — any of the characters in parentheses, in this case either a comma or a period.
  4. [5-9] — one digit from 5 to 9 inclusive.
  5. \d{0,}? is any digit; {0,}? — lazily repeated 0 or more times.
  6. $ — the end of the line (not to look for values in any part of the text).

Numbers from 1 to ∞ inclusive

Let's try to write down in words what we want to get.

  1. The number must necessarily start with a digit from 1-9, because numbers less than 1 are not suitable for us.
  2. Optionally, there can be any additional digits after.
  3. There may not necessarily be a "tail" in the form of a fraction.

^[1-9]\d{0,}?([.,]\ d{1,}?){0,1}$ — it turned out such a regular:

  1. ^ — the beginning of the line (so as not to pick out values in any part of the text).
  2. [1-9] — digit from 1 to 9 inclusive.
  3. \d{0,}? — any digit; {0,}? — lazily repeated 0 or more times.
  4. ([.,]\d{1,}?){0,1} — [.,] — a dot or comma followed by \d{1,}? one or more digits lazily repeated. The expression was wrapped in parentheses in order to set the whole rule {0,1} — zero or one time for it.
  5. $ — the end of the line (not to look for values in any part of the text).

All together

The simplest thing left is to combine several regular expressions into one. It's quite simple, you need to wrap each expression in a separate group using parentheses (expression), and then put a sign OR — | between the expressions. You’ll get (expression 1)|(expression 2).

The result: (^0[.,][5-9]\ d{0,}?$)|(^[1-9]\d{0,}?([.,]\d{1,}?){0,1}$)

Notes

Some parts of the expression can be changed for the convenience of writing, reading or perception:

  • \d — can be replaced by [0-9]
  • {0,} — can be replaced by *
  • {1,} — can be replaced by +

Also, for convenience, ^ and $ can be taken out of brackets and left in a single copy:

^((0[.,][5-9]\d{0,}?)|([1-9]\d{0,}?([.,]\d{1,}?){0,1}))$