Regular expression for number range

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
Roberto Madro R.Programmer AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Terry WoodsIT GuruCommented:
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.
0
frankhelkCommented:
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.
1
Roberto Madro R.Programmer AnalystAuthor 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.
0
frankhelkCommented:
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 ...)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roberto Madro R.Programmer AnalystAuthor Commented:
The solution worked in my test environment and I see no reason why it wouldn't in production, many thanks frankhelk.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Regular Expressions

From novice to tech pro — start learning today.