Regular expression for number range

Roberto Madro R.
Roberto Madro R. used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Terry WoodsIT Guru
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.
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.
Ok ... in that case - the field should accept numeric input only, with one decimal place - you could use the regex to ensure the rough frame as far as simple textual checking makes sense. Such a regex could be
^\d{2,3}(\.\d?)?$

Open in new window

Meaning (analyzed by the regex tool Expresso, see www.ultrapico.com):
Expresso---Regex-Analysis.pngThat would cover the formats you've given and i.e. "60" or "72." as well. Depending on your implementation you should omit ^ and $ from the regex. But it would match 01.0 and 999.9 as well.

Anyhow, everything that matches the expression above should be fit to be casted from String to a numeric variable (i.e. to a Double) and get checked for the numeric boundaries the way I've outlined before.

Old saying: If you have a hammer, almost everything looks like a nail.
(but there are good reasons to use a screwdriver for a screw instead ...)
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial