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

line edit validator number regex

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 4 Posters 1.7k Views
  • 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.
  • U Offline
    U Offline
    user4592357
    wrote on 17 Dec 2020, 17:49 last edited by
    #1

    i need to have validator on my line edit.
    the format of the acceptable input is as following: list of <number>s, or list of <number-number>s, or list of both, e.g. 4-8 10 34-100 222
    i have compiled the following regexp: ([1-9]\d*(-[1-9]\d*)?\s*)+
    but it falsely allows 5-55-55-55-555, while i need just 5-55 and not allow hyphen after second number
    what's the correct regex to use?

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 17 Dec 2020, 18:08 last edited by
      #2

      Hi,

      You are over-engineering it:

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

      Should do it

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

      U 1 Reply Last reply 17 Dec 2020, 18:29
      3
      • S SGaist
        17 Dec 2020, 18:08

        Hi,

        You are over-engineering it:

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

        Should do it

        U Offline
        U Offline
        user4592357
        wrote on 17 Dec 2020, 18:29 last edited by
        #3

        @SGaist
        sorry, i forgot to mention, i need only positive numbers.
        and that regex works for just one item, but i need to validate a list.

        J 1 Reply Last reply 17 Dec 2020, 19:05
        0
        • U user4592357
          17 Dec 2020, 18:29

          @SGaist
          sorry, i forgot to mention, i need only positive numbers.
          and that regex works for just one item, but i need to validate a list.

          J Offline
          J Offline
          JonB
          wrote on 17 Dec 2020, 19:05 last edited by
          #4

          @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 1 Reply Last reply 17 Dec 2020, 19:09
          0
          • J JonB
            17 Dec 2020, 19:05

            @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 17 Dec 2020, 19:09 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).

            J 1 Reply Last reply 17 Dec 2020, 19:17
            0
            • U user4592357
              17 Dec 2020, 19:09

              @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).

              J Offline
              J Offline
              JonB
              wrote on 17 Dec 2020, 19:17 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 17 Dec 2020, 19:37
              1
              • J JonB
                17 Dec 2020, 19:17

                @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 17 Dec 2020, 19:37 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?

                J 1 Reply Last reply 17 Dec 2020, 19:42
                0
                • U user4592357
                  17 Dec 2020, 19:37

                  @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?

                  J Offline
                  J Offline
                  JonB
                  wrote on 17 Dec 2020, 19:42 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
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 17 Dec 2020, 19:57 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

                    J 1 Reply Last reply 17 Dec 2020, 20:06
                    3
                    • S SGaist
                      17 Dec 2020, 19:57

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

                      J Offline
                      J Offline
                      JonB
                      wrote on 17 Dec 2020, 20:06 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 18 Dec 2020, 05:27 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.

                        J 1 Reply Last reply 18 Dec 2020, 10:04
                        0
                        • U user4592357
                          18 Dec 2020, 05:27

                          @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.

                          J Offline
                          J Offline
                          JonB
                          wrote on 18 Dec 2020, 10:04 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?

                          O 1 Reply Last reply 18 Dec 2020, 10:16
                          0
                          • J JonB
                            18 Dec 2020, 10:04

                            @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?

                            O Offline
                            O Offline
                            ODБOï
                            wrote on 18 Dec 2020, 10:16 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.

                            J 1 Reply Last reply 18 Dec 2020, 10:20
                            1
                            • O ODБOï
                              18 Dec 2020, 10:16

                              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.

                              J Offline
                              J Offline
                              JonB
                              wrote on 18 Dec 2020, 10:20 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 18 Dec 2020, 10:41
                              2
                              • J JonB
                                18 Dec 2020, 10:20

                                @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 18 Dec 2020, 10:41 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.

                                J 1 Reply Last reply 18 Dec 2020, 10:49
                                0
                                • U user4592357
                                  18 Dec 2020, 10:41

                                  @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.

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 18 Dec 2020, 10:49 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 18 Dec 2020, 10:54
                                  0
                                  • J JonB
                                    18 Dec 2020, 10:49

                                    @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 18 Dec 2020, 10:54 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.

                                    J 1 Reply Last reply 18 Dec 2020, 11:00
                                    0
                                    • U user4592357
                                      18 Dec 2020, 10:54

                                      @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.

                                      J Offline
                                      J Offline
                                      JonB
                                      wrote on 18 Dec 2020, 11:00 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 18 Dec 2020, 11:28
                                      3
                                      • J JonB
                                        18 Dec 2020, 11:00

                                        @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 18 Dec 2020, 11:28 last edited by
                                        #19

                                        @JonB
                                        ok got it, thanks for explanation!

                                        1 Reply Last reply
                                        0

                                        9/19

                                        17 Dec 2020, 19:57

                                        • Login

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