Roberto Madro R.
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.
Thx
Thx
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
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.
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
...
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.
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The solution worked in my test environment and I see no reason why it wouldn't in production, many thanks frankhelk.
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.