maztt
maztt

Reputation: 12294

Regular expression for checking the range of numeric values

I want to have a range between 0 to 65536 , what would be the regular expression for that?

Upvotes: 0

Views: 2066

Answers (3)

Spudley
Spudley

Reputation: 168793

See also this question, which asks pretty much the same thing, and got pretty much the same answer. (ie don't use regex for this!)

Upvotes: 1

LukeH
LukeH

Reputation: 269558

A regular expression really isn't well suited to this sort of validation. Gareth's answer provides a much more sensible solution.

If, for some reason, you absolutely have to use a regex then it will probably look something like this:

^(?:[0-5]?[0-9]{1,4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-6])$

Upvotes: 1

Gareth
Gareth

Reputation: 138150

Don't use a regular expression.

if(i >= 0 && i <= 65536)

Upvotes: 9

Related Questions