I'm trying to write some code to match numbers below certain bounds. I would much appreciate nay help somebody could give, I'm pretty lost with this one...

NB. I've checked the forums and can find similar questions to this but they are not really helping. Regex is fairly tricky!

  • $0 < x < 500$
  • $500 \leq x < 1000$
  • $1000 \leq x < 1499$
  • $1500 \leq x < 1999$
  • $2000 \leq x < 2500$

  • $2500 \leq x < 5000$

  • $ x \ge 5000$

Please not that $~x~$ is a real number, and therefore could be a decimal. (e.g. $100.4924$).

The furthest I have got is the below. The idea was to match numbers for each bound, and then write a separate criterion to exclude numbers below another certain bound.

I can use two regex matches in the program I'm using. E.g for a regex match for the bound: $~500 \leq x < 1000~$, I could use a match to be "less than $1000$" and then a not a regex match for the bound: "Less than $500$"

Less than $500$


Less than $1000$


Less than $1500$


Less than $2500$


Less than $5000$

$[1-5]?[0-9]?[0-9]?[0-9]?( [0-9] )?$

The matches are fairly close, but are also returning matches for multiple leading digits - rather than just the $0$ or $1$ that the ?. is supposed to specify :/

  • $\begingroup$ Not a complete answer but you could start with something like this (?<!\d)([0-4]+\d{2}|\d{1,2})(?!\d) for numbers between 0 and 500 and work from there. This uses a negative lookbehind and lookahead and an or clause. $\endgroup$ – Lieven Keersmaekers Jul 25 '19 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.