We help IT Professionals succeed at work.

Regular expression for number range

436 Views
Last Modified: 2017-04-13
If I was to use the following regex, /\d\d\d\.\d/g, I can get a three digits number with a decimal 555.1, etc., but if I want to specify an input range a user enter, and that these number will always include a decimal, for example the minimum would be 70.3, and the max would be 110.5, how would I go about that.
Thx
Comment
Watch Question

Terry WoodsWeb Developer, specialising in WordPress
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
A regex pattern to cover a range needs to be custom built; there's no easy and obvious way to just tell it to match a range.

I think there is a tool available called RegexBuddy that will generate a pattern automatically for a given range, but it may cost to buy the tool.

For the example you've given, I think this pattern should work:

/7(0\.[3-9]|[1-9]\.\d)|[89]\d\.d|1(0\d.\d|10\.[0-5])/g

Open in new window


If you can make sense of it, you'll see the pattern is split into 3 main parts (with sub parts), the main parts being for the range from 70.3 to 70.9, the range from 80.0 to 99.99, and the range from 100.0 to 110.5.

Similar techniques can be used to generate a pattern for any given range I believe.
CERTIFIED EXPERT

Commented:
That's one of the examples where regular expressions should be used for somthing they are not been invented for.

The sole intention for regular expressions is to match text patterns. Series of characters that meet certain conditions. In that aspect regular expressions are the gold standard for search an replace actions ....

They're not intended in any way for matching things that are encoded with these character series, like i.e. numeric values or - worse - bit field encoded flags ... ;-). Regular expressions for matching such irregular things are usually extremely complex, a pain in the a** to develop, debug and understand, and in the end they're usually full of nasty loopholes.

If I had to check the value written in a string, I'd just extract it out of the string (for THAT you could easily use a - usually very simple - regex ), cast it into a decent numeric type (int, long, double, log double, whatever) and perform the checks in clear and understandable math statements like
(some statements to isolate the value part out of the string ... depens on format)
value = CDbl(valueString)
if (value >= 70.3) and (value <= 110.5) then
...
else
...

Open in new window


Besides of that, casting to a numeric value and math check is done blindingly fast ... evaluating a regex of that complexity would take very much more CPU cycles, slowing down your application isignificantly if it's in a often executed code region.
Roberto Madro R.Programmer Analyst

Author

Commented:
frankhelk, this is for an application that takes in one of its field the body temperature, and the application is designed to control that field with regular expression, so that the operator doesn't enter his girlfriend's name in the Temperature field, also, the field shouldn't accept a temperature lower than 60.1 or higher than 110.1.
CERTIFIED EXPERT
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
Roberto Madro R.Programmer Analyst

Author

Commented:
The solution worked in my test environment and I see no reason why it wouldn't in production, many thanks frankhelk.