Matching Floating Point Numbers Range with a Regular Expression
-
I have to check the user input in some input fields for a float value range with Regex. Here are my two float value ranges for which I have not yet found a solution: ``` Case 1. (ui >= 0.0001) & (ui <= 10000.0000) example ok: 0.0001, 10000.0000 example fail: 0.0000, 10000.0001 Case 2. (ui >= 0.0000) & (ui <= 65536.0000) example ok: 0.0000, 65536.0000 example fail: -0.0001, 65536.0001 I have been working on a solution for this for a long time. Unfortunately without success so far. I would be very happy if someone could help me.
-
I have to check the user input in some input fields for a float value range with Regex. Here are my two float value ranges for which I have not yet found a solution: ``` Case 1. (ui >= 0.0001) & (ui <= 10000.0000) example ok: 0.0001, 10000.0000 example fail: 0.0000, 10000.0001 Case 2. (ui >= 0.0000) & (ui <= 65536.0000) example ok: 0.0000, 65536.0000 example fail: -0.0001, 65536.0001 I have been working on a solution for this for a long time. Unfortunately without success so far. I would be very happy if someone could help me.
Don't use regular expressions for this. Parse the values as floating point numbers, and then compare them to your acceptable range.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Don't use regular expressions for this. Parse the values as floating point numbers, and then compare them to your acceptable range.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Also, if you use the library functions for parsing floating point values, they will/may consider the culture. In many European cultures, a decimal comma is used, not a decimal point. The point is used a digit group separator. Programming code literals (almost?) always follows the English tradition of decimal point, and comma as group separator. User input is different. You can't make all users switch to a different number syntax just because your program doesn't honor the local culture. A minor non-regex comment to the OP: If you intend the check
Case 2. (ui >= 0.0000) & (ui <= 65536.0000)
to verify that the number can be converted to a 16 bit uint, then it should be either (ui < 65536.0000) or (ui <= 65535.0000). (But then, I think it curious to verify a floating point number against the value limits of an integer type. A numeric value is either a measurement or a count. Re-interpreting a measurement as a count is a strange thing to do.)
Religious freedom is the freedom to say that two plus two make five.
-
Don't use regular expressions for this. Parse the values as floating point numbers, and then compare them to your acceptable range.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I have found a solution for case 1:
^((0[\,\.]000[1])|([1-9]\d{0,3}(?:[\,\.]\d{1,4}))|(?:10000(?:[\,\.]0{1,4})))?$
However, the expression is not as difficult as is said here. At the special request of a single person, the regular expression now also accepts "." and ",". I program in QT and QLineEdit can be configured very nicely and effectively with regex expressions. I would therefore be very reluctant to deviate from this. I am programming an LCR measuring bridge front end from Analog Devices (ADMX2001). The tDelay value range between 0.0000 and 65536.0000 is expected by the frontend and is also checked. And the value range does not fit into a uint16. Should I now discuss the specified value range with Analog Devices ? They must know in which way the bridge operates.
-
Don't use regular expressions for this. Parse the values as floating point numbers, and then compare them to your acceptable range.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Here is the solution for case 1: (ui >= 0.0000) & (ui <= 10000.0000): ^((0([\,\.]\d{0,4})?)|([1-9]\d{0,3}(?:[\,\.]\d{1,4})?)|(?:10000(?:[\,\.]0{1,4})))?$ (ui >= 0.0001) & (ui <= 10000.0000): ^((0[\,\.]000[1])|(0[\,\.]\d{0,3}[1-9])|([1-9]\d{0,3}(?:[\,\.]\d{1,4})?)|(?:10000(?:[\,\.]0{1,4})))?$ Maybe the solution will help someone else.
-
I have found a solution for case 1:
^((0[\,\.]000[1])|([1-9]\d{0,3}(?:[\,\.]\d{1,4}))|(?:10000(?:[\,\.]0{1,4})))?$
However, the expression is not as difficult as is said here. At the special request of a single person, the regular expression now also accepts "." and ",". I program in QT and QLineEdit can be configured very nicely and effectively with regex expressions. I would therefore be very reluctant to deviate from this. I am programming an LCR measuring bridge front end from Analog Devices (ADMX2001). The tDelay value range between 0.0000 and 65536.0000 is expected by the frontend and is also checked. And the value range does not fit into a uint16. Should I now discuss the specified value range with Analog Devices ? They must know in which way the bridge operates.
Dagobert1 wrote:
At the special request of a single person, the regular expression now also accepts "." and ",".
That isn't what they said. There are many users in many places that represent decimal numbers using a different form. So you either do not want to support them with your solution or you do.
-
Here is the solution for case 1: (ui >= 0.0000) & (ui <= 10000.0000): ^((0([\,\.]\d{0,4})?)|([1-9]\d{0,3}(?:[\,\.]\d{1,4})?)|(?:10000(?:[\,\.]0{1,4})))?$ (ui >= 0.0001) & (ui <= 10000.0000): ^((0[\,\.]000[1])|(0[\,\.]\d{0,3}[1-9])|([1-9]\d{0,3}(?:[\,\.]\d{1,4})?)|(?:10000(?:[\,\.]0{1,4})))?$ Maybe the solution will help someone else.
Dagobert1 wrote:
Maybe the solution will help someone else.
I have been using regular expressions extensively for decades and the way I would solve the problem was already suggested in a previous post. Parse the number into a floating point value and then validate it that way. Even when I have needed to provide a configurable validation I have designed it that way. It is not only less complex it is also going to be faster.
-
Here is the solution for case 1: (ui >= 0.0000) & (ui <= 10000.0000): ^((0([\,\.]\d{0,4})?)|([1-9]\d{0,3}(?:[\,\.]\d{1,4})?)|(?:10000(?:[\,\.]0{1,4})))?$ (ui >= 0.0001) & (ui <= 10000.0000): ^((0[\,\.]000[1])|(0[\,\.]\d{0,3}[1-9])|([1-9]\d{0,3}(?:[\,\.]\d{1,4})?)|(?:10000(?:[\,\.]0{1,4})))?$ Maybe the solution will help someone else.
Now try any remotely-complicated range - eg:
ui ≥ -19.4242 && ui ≤ 1337.4242
- and see how "easy" that is with a regex. :doh: Parsing the value as an appropriate type and then checking the range is far simpler and faster. And as has already been pointed out, it will handle culture-specific formatting much better.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
I have to check the user input in some input fields for a float value range with Regex. Here are my two float value ranges for which I have not yet found a solution: ``` Case 1. (ui >= 0.0001) & (ui <= 10000.0000) example ok: 0.0001, 10000.0000 example fail: 0.0000, 10000.0001 Case 2. (ui >= 0.0000) & (ui <= 65536.0000) example ok: 0.0000, 65536.0000 example fail: -0.0001, 65536.0001 I have been working on a solution for this for a long time. Unfortunately without success so far. I would be very happy if someone could help me.
If all you are doing is trying to validate (in a QLineEdit) that a floating point number is in a particular range, why don't you use a QDoubleValidator with it? This allows you to set range values[^].