Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. line edit validator number regex
Forum Updated to NodeBB v4.3 + New Features

line edit validator number regex

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 4 Posters 1.9k Views 3 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.
  • JonBJ JonB

    @user4592357
    @SGaist's pattern does not allow negative numbers. For a space-separated list, you could go for, say:

    R"^((\d+|\d+-\d+) *)+$"
    

    There are other ways, depending on whether you need captures.

    U Offline
    U Offline
    user4592357
    wrote on last edited by user4592357
    #5

    @JonB
    i need to exclude 0 as a number or range's start value, or a number starting with 0.

    will that pattern prevent inputting the case i mentioned in original post? (sorry i'm away from computer right now).

    JonBJ 1 Reply Last reply
    0
    • U user4592357

      @JonB
      i need to exclude 0 as a number or range's start value, or a number starting with 0.

      will that pattern prevent inputting the case i mentioned in original post? (sorry i'm away from computer right now).

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #6

      @user4592357
      So replace each \d token with something like ([1-9]\d*).

      If you want to start putting more constraints on, like say in the \d+-\d+ case you might want the right hand number to be greater than the the left hand one, it's time to pull out the matches and do some numeric comparisons in a more advanced validation.

      U 1 Reply Last reply
      1
      • JonBJ JonB

        @user4592357
        So replace each \d token with something like ([1-9]\d*).

        If you want to start putting more constraints on, like say in the \d+-\d+ case you might want the right hand number to be greater than the the left hand one, it's time to pull out the matches and do some numeric comparisons in a more advanced validation.

        U Offline
        U Offline
        user4592357
        wrote on last edited by
        #7

        @JonB
        yes sure I will replace the numbers.
        i just need to make sure that i the 5-55-55-55 pattern is prohibited by that regex, is it?

        JonBJ 1 Reply Last reply
        0
        • U user4592357

          @JonB
          yes sure I will replace the numbers.
          i just need to make sure that i the 5-55-55-55 pattern is prohibited by that regex, is it?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #8

          @user4592357
          You want me to test it for you?! Start using an on-line resource to design & test the reg exs you want to use, e.g. https://regex101.com/ .

          1 Reply Last reply
          2
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #9

            You also have the regular expression tool that you can build and will give you the exact string you need in your code.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            JonBJ 1 Reply Last reply
            3
            • SGaistS SGaist

              You also have the regular expression tool that you can build and will give you the exact string you need in your code.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #10

              @SGaist
              Excellent. But @user4592357 will probably have to return to his computer to achieve that :)

              1 Reply Last reply
              0
              • U Offline
                U Offline
                user4592357
                wrote on last edited by
                #11

                @SGaist @JonB
                thanks for all info.
                unfortunately, ^((\d+|\d+-\d+) *)+$ regex still allows hyphen after 5-555.
                i just need to regexp validator for my input, nothing to match for now.

                JonBJ 1 Reply Last reply
                0
                • U user4592357

                  @SGaist @JonB
                  thanks for all info.
                  unfortunately, ^((\d+|\d+-\d+) *)+$ regex still allows hyphen after 5-555.
                  i just need to regexp validator for my input, nothing to match for now.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #12

                  @user4592357 said in line edit validator number regex:

                  unfortunately, ^((\d+|\d+-\d+) *)+$ regex still allows hyphen after 5-555.

                  Pardon?
                  5-555- gives Your regular expression does not match the subject string..

                  Instead of making us guess what input you have in mind, could you please test with one of the reg ex validators we referenced and paste precisely what string you claim it does/does not match?

                  ODБOïO 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @user4592357 said in line edit validator number regex:

                    unfortunately, ^((\d+|\d+-\d+) *)+$ regex still allows hyphen after 5-555.

                    Pardon?
                    5-555- gives Your regular expression does not match the subject string..

                    Instead of making us guess what input you have in mind, could you please test with one of the reg ex validators we referenced and paste precisely what string you claim it does/does not match?

                    ODБOïO Offline
                    ODБOïO Offline
                    ODБOï
                    wrote on last edited by
                    #13

                    hi
                    @JonB said in line edit validator number regex:

                    5-555- gives Your regular expression does not match the subject string..

                    If you write another number after the "5-555-" , then it will be accepted, ex : 5-555-8
                    OP only wants to accept groupes of 2 numbers separated by a hyphen.

                    JonBJ 1 Reply Last reply
                    1
                    • ODБOïO ODБOï

                      hi
                      @JonB said in line edit validator number regex:

                      5-555- gives Your regular expression does not match the subject string..

                      If you write another number after the "5-555-" , then it will be accepted, ex : 5-555-8
                      OP only wants to accept groupes of 2 numbers separated by a hyphen.

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #14

                      @LeLev
                      Then I wish he had said so and given such an example, instead of

                      still allows hyphen after 5-555.

                      I will look at this now and offer an alternative in a few minutes time...
                      ...like either of:

                      ^((\d+|\d+-\d+)\b *)+$
                      
                      ^(\d+(-\d+)?\b *)+$
                      
                      U 1 Reply Last reply
                      2
                      • JonBJ JonB

                        @LeLev
                        Then I wish he had said so and given such an example, instead of

                        still allows hyphen after 5-555.

                        I will look at this now and offer an alternative in a few minutes time...
                        ...like either of:

                        ^((\d+|\d+-\d+)\b *)+$
                        
                        ^(\d+(-\d+)?\b *)+$
                        
                        U Offline
                        U Offline
                        user4592357
                        wrote on last edited by user4592357
                        #15

                        @JonB
                        thanks!
                        anyways, i wanna understand why the regex with \b allows 55-555-? how does it match/validate?
                        because for that same string, the line edit's hasAcceptableInput() returns false.

                        JonBJ 1 Reply Last reply
                        0
                        • U user4592357

                          @JonB
                          thanks!
                          anyways, i wanna understand why the regex with \b allows 55-555-? how does it match/validate?
                          because for that same string, the line edit's hasAcceptableInput() returns false.

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #16

                          @user4592357

                          anyways, i wanna understand why the regex with \b allows 55-555-? how does it match/validate?

                          Neither of the two regexs I have given with \b allow 55-555-. As tested at rex101.com.

                          I am finding your questions/statements very hard to comprehend....

                          U 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @user4592357

                            anyways, i wanna understand why the regex with \b allows 55-555-? how does it match/validate?

                            Neither of the two regexs I have given with \b allow 55-555-. As tested at rex101.com.

                            I am finding your questions/statements very hard to comprehend....

                            U Offline
                            U Offline
                            user4592357
                            wrote on last edited by
                            #17

                            @JonB
                            sorry, i mean with previous (((\d+|\d+-\d+) *)+) regex, the validator allowed 55-555-.
                            which part of the regex string did match with the hyphen after 55-555? i want to understand the logic for matching it.

                            JonBJ 1 Reply Last reply
                            0
                            • U user4592357

                              @JonB
                              sorry, i mean with previous (((\d+|\d+-\d+) *)+) regex, the validator allowed 55-555-.
                              which part of the regex string did match with the hyphen after 55-555? i want to understand the logic for matching it.

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #18

                              @user4592357
                              So when you said

                              why the regex with \b allows

                              you meant

                              why the regex without \b allows

                              ! :)

                              Regex: ^((\d+|\d+-\d+) *)+$
                              Input: 5-555-8

                              You want to know why the input does match here. That's because in order to force a match if it possibly can, which is how reg exs work, this gets treated as though it were:
                              5-55 5-8
                              i.e. two separate digits-digits sequences. So I put in the \b word-boundary token in ^((\d+|\d+-\d+)\b *)+$ to prevent it splitting in the middle of a sequence of consecutive digits.

                              U 1 Reply Last reply
                              3
                              • JonBJ JonB

                                @user4592357
                                So when you said

                                why the regex with \b allows

                                you meant

                                why the regex without \b allows

                                ! :)

                                Regex: ^((\d+|\d+-\d+) *)+$
                                Input: 5-555-8

                                You want to know why the input does match here. That's because in order to force a match if it possibly can, which is how reg exs work, this gets treated as though it were:
                                5-55 5-8
                                i.e. two separate digits-digits sequences. So I put in the \b word-boundary token in ^((\d+|\d+-\d+)\b *)+$ to prevent it splitting in the middle of a sequence of consecutive digits.

                                U Offline
                                U Offline
                                user4592357
                                wrote on last edited by
                                #19

                                @JonB
                                ok got it, thanks for explanation!

                                1 Reply Last reply
                                0

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved