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. QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,1}");
Forum Updated to NodeBB v4.3 + New Features

QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,1}");

Scheduled Pinned Locked Moved Solved General and Desktop
16 Posts 3 Posters 1.4k 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.
  • gfxxG Offline
    gfxxG Offline
    gfxx
    wrote on last edited by gfxx
    #1
    QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,3}");
        QRegExpValidator v(rx, 0);
        int pos = 0;
    

    never use first ... try to "filter" user input for write on QlineEdit the coordinate of system .... for me these world need to be VALID:

    x200,5 Y900
    X50
    X-50
    Y-1235.5 x-3

    and these must be UNVALID

    a300 Y800
    x50000
    y70.00000 X60
    y90 X-30 Y70
    X-50,25698
    Y-1235.5x-3

    try to use these link validator site helph

    with no complete success ... because previous code for negative and positive number was not accept .... these one was accept ((C|X|Y|Z|c|x|y|z)\d{0,3}(.\d+)?\s*$) (not with double \ but with single one ... because language) ...
    but work only with one only instance like x400 .... with x50 y60 and title post code not run correctly .....

    bkt

    1 Reply Last reply
    0
    • gfxxG gfxx

      @JonB said in QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,1}");:

      ^([xyuv]-?\d{1,4}([,.]\d{1,1})?)( \1)?( \1)?( \1)?$

      these match only exactly the same occorence of previous ... so

      these match:
      x100 x100 x100
      these not match (but needed)
      x10 y100 u3000

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

      @gfxx
      Ohhhh :( I did wonder about that, but didn't test :) That's a shame, for this particular purpose.....

      It's regrettable that you cannot do something about the whitespace separator which prevents you from using a {1,4}-type thing for your pattern. The problem is that you only want to allow spaces between patterns. But how does, say,

      ^([xyuv]-?\d{1,4}([,.]\d)? ?){1,4}$
      

      perform? The usual way with reg exs is that matches are greedy, using as much as possible e.g. when it has a choice whether to put a character in the match to its left rather than saving it for a match to the right, provided both can be done. You might find this pattern works for your inputs.

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

        Hi,

        Unless you are using Qt 4, please move to QRegularExpression and and QRegularExpressionValidator. QRegExp and its validator were deprecated in Qt 5.

        Based on your examples, the following regexp, if used with the case insensitive option should work:

        ^[xy]-?\d{1,4}([,.]\d{1,3})?( [xy]-?\d{1,4}([,.]\d{1,3})?)?$

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

        gfxxG 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          Unless you are using Qt 4, please move to QRegularExpression and and QRegularExpressionValidator. QRegExp and its validator were deprecated in Qt 5.

          Based on your examples, the following regexp, if used with the case insensitive option should work:

          ^[xy]-?\d{1,4}([,.]\d{1,3})?( [xy]-?\d{1,4}([,.]\d{1,3})?)?$

          gfxxG Offline
          gfxxG Offline
          gfxx
          wrote on last edited by gfxx
          #3

          @SGaist said in QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,1}");:

          ^[xy]-?\d{1,4}([,.]\d{1,3})?( [xy]-?\d{1,4}([,.]\d{1,3})?)?$

          Thanks .... for upgrade version too .... so if need at least check 4 coordinates, based on your code ... these seems to work .... but is good too? (need one oly digit after "." ...

          ^[xyuv]-?\d{1,4}([,.]\d{1,1})?( [xyuv]-?\d{1,4}([,.]\d{1,1})?)?( [xyuv]-?\d{1,4}([,.]\d{1,1})?( [xyuv]-?\d{1,4}([,.]\d{1,1})?))$
          

          regards

          bkt

          SGaistS JonBJ 2 Replies Last reply
          0
          • gfxxG gfxx

            @SGaist said in QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,1}");:

            ^[xy]-?\d{1,4}([,.]\d{1,3})?( [xy]-?\d{1,4}([,.]\d{1,3})?)?$

            Thanks .... for upgrade version too .... so if need at least check 4 coordinates, based on your code ... these seems to work .... but is good too? (need one oly digit after "." ...

            ^[xyuv]-?\d{1,4}([,.]\d{1,1})?( [xyuv]-?\d{1,4}([,.]\d{1,1})?)?( [xyuv]-?\d{1,4}([,.]\d{1,1})?( [xyuv]-?\d{1,4}([,.]\d{1,1})?))$
            

            regards

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #4

            You can simply remove the {1,1}s. It just clutters the regex for no benefit.

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

            1 Reply Last reply
            0
            • gfxxG gfxx

              @SGaist said in QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,1}");:

              ^[xy]-?\d{1,4}([,.]\d{1,3})?( [xy]-?\d{1,4}([,.]\d{1,3})?)?$

              Thanks .... for upgrade version too .... so if need at least check 4 coordinates, based on your code ... these seems to work .... but is good too? (need one oly digit after "." ...

              ^[xyuv]-?\d{1,4}([,.]\d{1,1})?( [xyuv]-?\d{1,4}([,.]\d{1,1})?)?( [xyuv]-?\d{1,4}([,.]\d{1,1})?( [xyuv]-?\d{1,4}([,.]\d{1,1})?))$
              

              regards

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

              @gfxx
              It's up to you what your rules are, but why do you now have both [xyuv] and [xyzc]? You are introducing new restrictions, and your reg ex is getting more & more complicated!

              gfxxG 1 Reply Last reply
              0
              • JonBJ JonB

                @gfxx
                It's up to you what your rules are, but why do you now have both [xyuv] and [xyzc]? You are introducing new restrictions, and your reg ex is getting more & more complicated!

                gfxxG Offline
                gfxxG Offline
                gfxx
                wrote on last edited by gfxx
                #6

                @JonB sorry zc is an error becuse previuos name of coordinates .... correct just now. .... simply need to have 4 item of coordinates no more but can be less ....

                bkt

                SGaistS 1 Reply Last reply
                0
                • gfxxG gfxx

                  @JonB sorry zc is an error becuse previuos name of coordinates .... correct just now. .... simply need to have 4 item of coordinates no more but can be less ....

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @gfxx can you give an exact example of what you can get ?

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

                  gfxxG 1 Reply Last reply
                  1
                  • SGaistS SGaist

                    @gfxx can you give an exact example of what you can get ?

                    gfxxG Offline
                    gfxxG Offline
                    gfxx
                    wrote on last edited by
                    #8

                    @SGaist

                    for me these world need to be VALID:

                    x200,5 Y900 u900 v-10.9
                    X50
                    X-50
                    Y-1235.5 x-3

                    and these must be UNVALID

                    a300 Y800 u600 v200 x3000 (because 5 coord.)
                    x50000
                    y70.00000 X60
                    y90 X-30 Y70
                    X-50,25698
                    Y-1235.5x-3

                    the same as previous but with 4 cood. The code suggested work ... only ask if possible make it better .... if tomorrow need to check 8 coord. in one only time .... the code used become a very long string (more bigger more error) .... only these ... thanks

                    bkt

                    JonBJ 1 Reply Last reply
                    0
                    • gfxxG gfxx

                      @SGaist

                      for me these world need to be VALID:

                      x200,5 Y900 u900 v-10.9
                      X50
                      X-50
                      Y-1235.5 x-3

                      and these must be UNVALID

                      a300 Y800 u600 v200 x3000 (because 5 coord.)
                      x50000
                      y70.00000 X60
                      y90 X-30 Y70
                      X-50,25698
                      Y-1235.5x-3

                      the same as previous but with 4 cood. The code suggested work ... only ask if possible make it better .... if tomorrow need to check 8 coord. in one only time .... the code used become a very long string (more bigger more error) .... only these ... thanks

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

                      @gfxx
                      Leaving aside the fact that you change your examples/code as we go along, e.g. introduction of [xyuv], it's hard to keep up.

                      To keep it manageable, you need to use regular expression back references. This will allow you to insert the "complicated" pattern (for each individual space-separated entry) just once, and then use again a number of times without having to repeat it. You can easily change between, say, 2 occurrences and 4 occurrences.

                      This is done by surrounding that pattern segment in parentheses ((...)) and then using \digit (\1) to call it back. Consider:

                      ([a-zA-Z]+) \1 \1
                      

                      This means 3 words, with a space between each (not at beginning or end).

                      So your pattern for 4 will be something like

                      ^([xyuv]-?\d{1,4}([,.]\d{1,1})?)( \1)?( \1)?( \1)?$
                      

                      A lot easier, right?!

                      I have not verified that Qt QRegularExpression accepts these back references, though I am expecting it to. You should test on a small example and then use it if it works.

                      gfxxG 1 Reply Last reply
                      0
                      • JonBJ JonB

                        @gfxx
                        Leaving aside the fact that you change your examples/code as we go along, e.g. introduction of [xyuv], it's hard to keep up.

                        To keep it manageable, you need to use regular expression back references. This will allow you to insert the "complicated" pattern (for each individual space-separated entry) just once, and then use again a number of times without having to repeat it. You can easily change between, say, 2 occurrences and 4 occurrences.

                        This is done by surrounding that pattern segment in parentheses ((...)) and then using \digit (\1) to call it back. Consider:

                        ([a-zA-Z]+) \1 \1
                        

                        This means 3 words, with a space between each (not at beginning or end).

                        So your pattern for 4 will be something like

                        ^([xyuv]-?\d{1,4}([,.]\d{1,1})?)( \1)?( \1)?( \1)?$
                        

                        A lot easier, right?!

                        I have not verified that Qt QRegularExpression accepts these back references, though I am expecting it to. You should test on a small example and then use it if it works.

                        gfxxG Offline
                        gfxxG Offline
                        gfxx
                        wrote on last edited by
                        #10

                        @JonB said in QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,1}");:

                        ^([xyuv]-?\d{1,4}([,.]\d{1,1})?)( \1)?( \1)?( \1)?$

                        these match only exactly the same occorence of previous ... so

                        these match:
                        x100 x100 x100
                        these not match (but needed)
                        x10 y100 u3000

                        bkt

                        JonBJ 1 Reply Last reply
                        1
                        • gfxxG gfxx

                          @JonB said in QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,1}");:

                          ^([xyuv]-?\d{1,4}([,.]\d{1,1})?)( \1)?( \1)?( \1)?$

                          these match only exactly the same occorence of previous ... so

                          these match:
                          x100 x100 x100
                          these not match (but needed)
                          x10 y100 u3000

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

                          @gfxx
                          Ohhhh :( I did wonder about that, but didn't test :) That's a shame, for this particular purpose.....

                          It's regrettable that you cannot do something about the whitespace separator which prevents you from using a {1,4}-type thing for your pattern. The problem is that you only want to allow spaces between patterns. But how does, say,

                          ^([xyuv]-?\d{1,4}([,.]\d)? ?){1,4}$
                          

                          perform? The usual way with reg exs is that matches are greedy, using as much as possible e.g. when it has a choice whether to put a character in the match to its left rather than saving it for a match to the right, provided both can be done. You might find this pattern works for your inputs.

                          gfxxG 1 Reply Last reply
                          1
                          • JonBJ JonB

                            @gfxx
                            Ohhhh :( I did wonder about that, but didn't test :) That's a shame, for this particular purpose.....

                            It's regrettable that you cannot do something about the whitespace separator which prevents you from using a {1,4}-type thing for your pattern. The problem is that you only want to allow spaces between patterns. But how does, say,

                            ^([xyuv]-?\d{1,4}([,.]\d)? ?){1,4}$
                            

                            perform? The usual way with reg exs is that matches are greedy, using as much as possible e.g. when it has a choice whether to put a character in the match to its left rather than saving it for a match to the right, provided both can be done. You might find this pattern works for your inputs.

                            gfxxG Offline
                            gfxxG Offline
                            gfxx
                            wrote on last edited by gfxx
                            #12

                            @JonB said in QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,1}");:

                            Ohhhh :( I did wonder about that, but didn't test :) That's a shame, for this particular purpose.....
                            It's regrettable that you cannot do something about the whitespace separator which prevents you from using a {1,4}-type thing for your pattern. The problem is that you only want to allow spaces between patterns.

                            real thanks .... because try a lot of combination ... think anly not test was these:

                            ^([xyuv]-?\d{1,4}([,.]\d)? ?){1,4}$

                            where "? (spaces) ?" make the differences .... but exactly where find some instructions and "example" about these "markup" (sorry but not know how can call these sort of languages regexp)?

                            bkt

                            JonBJ 1 Reply Last reply
                            0
                            • gfxxG gfxx has marked this topic as solved on
                            • gfxxG gfxx

                              @JonB said in QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,1}");:

                              Ohhhh :( I did wonder about that, but didn't test :) That's a shame, for this particular purpose.....
                              It's regrettable that you cannot do something about the whitespace separator which prevents you from using a {1,4}-type thing for your pattern. The problem is that you only want to allow spaces between patterns.

                              real thanks .... because try a lot of combination ... think anly not test was these:

                              ^([xyuv]-?\d{1,4}([,.]\d)? ?){1,4}$

                              where "? (spaces) ?" make the differences .... but exactly where find some instructions and "example" about these "markup" (sorry but not know how can call these sort of languages regexp)?

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

                              @gfxx
                              My solution is a bit "cheaty" or "lucky". Because the space-separator has to be made optional via ? (that is the space-?), so as to allow for it not being at the end (or beginning) or the whole pattern, my pattern has a "danger" of allowing the stuff before it to "run into" the stuff after it, by choosing to match the space zero times for the ?. But, as I said, for your inputs it may not be able to do that, and can only match the way we want. Which seems to be the case.

                              As for where/how to learn regular expressions: I have done them all my lifetime :) It's an art as much as a science. There are various reference pages all over the web --- the Qt docs for regular expressions may even refer you to one. And play with things at https://regex101.com/.

                              gfxxG 1 Reply Last reply
                              1
                              • JonBJ JonB

                                @gfxx
                                My solution is a bit "cheaty" or "lucky". Because the space-separator has to be made optional via ? (that is the space-?), so as to allow for it not being at the end (or beginning) or the whole pattern, my pattern has a "danger" of allowing the stuff before it to "run into" the stuff after it, by choosing to match the space zero times for the ?. But, as I said, for your inputs it may not be able to do that, and can only match the way we want. Which seems to be the case.

                                As for where/how to learn regular expressions: I have done them all my lifetime :) It's an art as much as a science. There are various reference pages all over the web --- the Qt docs for regular expressions may even refer you to one. And play with things at https://regex101.com/.

                                gfxxG Offline
                                gfxxG Offline
                                gfxx
                                wrote on last edited by
                                #14

                                @JonB "cheaty" or "lucky" not important if work as work .... maybe you are an artist in these things and not a scientist .... but is really important?

                                Thanks a lot.

                                bkt

                                JonBJ 1 Reply Last reply
                                0
                                • gfxxG gfxx

                                  @JonB "cheaty" or "lucky" not important if work as work .... maybe you are an artist in these things and not a scientist .... but is really important?

                                  Thanks a lot.

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

                                  @gfxx said in QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,1}");:

                                  but is really important?

                                  Sadly, yes. I don't mean to burst our bubble, but I think it will accept this:

                                  x10y100
                                  

                                  Note that there is no space before the y. You may not be happy with this....

                                  gfxxG 1 Reply Last reply
                                  1
                                  • JonBJ JonB

                                    @gfxx said in QRegExp rx("[((X|Y|x|y)\\d{0,3}(\\.\\d+)?\\S$)]{0,1}");:

                                    but is really important?

                                    Sadly, yes. I don't mean to burst our bubble, but I think it will accept this:

                                    x10y100
                                    

                                    Note that there is no space before the y. You may not be happy with this....

                                    gfxxG Offline
                                    gfxxG Offline
                                    gfxx
                                    wrote on last edited by
                                    #16

                                    @JonB verified .... these is ok for me. thanks a lot

                                    bkt

                                    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