Avatar of Roberto Madro R.
Roberto Madro R.
Flag for United States of America asked on

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.
Regular Expressions

Avatar of undefined
Last Comment
Roberto Madro R.

8/22/2022 - Mon
Terry Woods

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:


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.
Frank Helk

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

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.

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.
Your help has saved me hundreds of hours of internet surfing.
Frank Helk

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Roberto Madro R.

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