Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. Matching Floating Point Numbers Range with a Regular Expression

Matching Floating Point Numbers Range with a Regular Expression

Scheduled Pinned Locked Moved Regular Expressions
regexdesignhelptutorial
9 Posts 5 Posters 51 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dagobert1
    wrote on last edited by
    #1

    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.

    Richard DeemingR P 2 Replies Last reply
    0
    • D Dagobert1

      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.

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      T D 3 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        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

        T Offline
        T Offline
        trønderen
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          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

          D Offline
          D Offline
          Dagobert1
          wrote on last edited by
          #4

          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.

          J 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            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

            D Offline
            D Offline
            Dagobert1
            wrote on last edited by
            #5

            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.

            J Richard DeemingR 2 Replies Last reply
            0
            • D Dagobert1

              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.

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • D Dagobert1

                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.

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • D Dagobert1

                  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.

                  Richard DeemingR Offline
                  Richard DeemingR Offline
                  Richard Deeming
                  wrote on last edited by
                  #8

                  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

                  "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

                  1 Reply Last reply
                  0
                  • D Dagobert1

                    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.

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    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[^].

                    Advanced TypeScript Programming Projects

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups