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 Global Flag
Forum Updated to NodeBB v4.3 + New Features

QRegExp Global Flag

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.2k 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.
  • webzoidW Offline
    webzoidW Offline
    webzoid
    wrote on last edited by
    #1

    Is there any way to set the global flag for a regular expression using QRegExp?

    This pattern:

    \(([^()]*)\)

    Matching against the following:

    (((119.7500182 25.3, 119.7730556 25.3, 119.7730556 25.3833333, 119.9672222 25.3833333, 119.9672222 25.1583333, 119.6125 25.1583333, 119.6125 25.3, 119.7500182 25.3)), ((119.8672912 25.8163889, 119.9672222 25.8163889, 119.9672222 25.6916667, 119.8561111 25.6916667, 119.8561111 25.8163889, 119.8672912 25.8163889)), ((119.4675 25.6008704, 119.4675 25.8163889, 119.5633333 25.8163889, 119.5633333 25.65, 119.5833333 25.65, 119.5833333 25.4805556, 119.6033333 25.4805556, 119.6033333 25.4316667, 119.4675 25.4316667, 119.4675 25.6008704)))
    

    Should produce 3 capture groups (well, when testing on various online RegEx processors) however Qt only manages 1 capture group.

    Disabling the global flag on the various online processors replicates the same behaviour as Qt.

    JonBJ 1 Reply Last reply
    0
    • webzoidW webzoid

      Is there any way to set the global flag for a regular expression using QRegExp?

      This pattern:

      \(([^()]*)\)

      Matching against the following:

      (((119.7500182 25.3, 119.7730556 25.3, 119.7730556 25.3833333, 119.9672222 25.3833333, 119.9672222 25.1583333, 119.6125 25.1583333, 119.6125 25.3, 119.7500182 25.3)), ((119.8672912 25.8163889, 119.9672222 25.8163889, 119.9672222 25.6916667, 119.8561111 25.6916667, 119.8561111 25.8163889, 119.8672912 25.8163889)), ((119.4675 25.6008704, 119.4675 25.8163889, 119.5633333 25.8163889, 119.5633333 25.65, 119.5833333 25.65, 119.5833333 25.4805556, 119.6033333 25.4805556, 119.6033333 25.4316667, 119.4675 25.4316667, 119.4675 25.6008704)))
      

      Should produce 3 capture groups (well, when testing on various online RegEx processors) however Qt only manages 1 capture group.

      Disabling the global flag on the various online processors replicates the same behaviour as Qt.

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

      @webzoid said in QRegExp Global Flag:

      Various ways, e.g. http://doc.qt.io/qt-5/qregexp.html#cap, http://doc.qt.io/qt-5/qregexp.html#capturedTexts. You don't specify "global", you get the captures anyway. So what makes you say "Should produce 3 capture groups [but does not]"?

      Also (since someone else will doubtless say this to you):

      Note: In Qt 5, the new QRegularExpression class provides a Perl compatible implementation of regular expressions and is recommended in place of QRegExp.

      webzoidW 1 Reply Last reply
      2
      • JonBJ JonB

        @webzoid said in QRegExp Global Flag:

        Various ways, e.g. http://doc.qt.io/qt-5/qregexp.html#cap, http://doc.qt.io/qt-5/qregexp.html#capturedTexts. You don't specify "global", you get the captures anyway. So what makes you say "Should produce 3 capture groups [but does not]"?

        Also (since someone else will doubtless say this to you):

        Note: In Qt 5, the new QRegularExpression class provides a Perl compatible implementation of regular expressions and is recommended in place of QRegExp.

        webzoidW Offline
        webzoidW Offline
        webzoid
        wrote on last edited by
        #3

        @JonB Unfortunately, I'm not getting the captures - well, not all 3 as I expect.

        The reason I expect them is that when testing using the online tools (using various RegEx "flavours"), I get 3 captures. Here's a link showing just that:

        https://regex101.com/r/ZlzWNM/3

        If I then try the same pattern and text in the regexp Qt example project, I only get 1 capture.

        JonBJ 1 Reply Last reply
        0
        • webzoidW webzoid

          @JonB Unfortunately, I'm not getting the captures - well, not all 3 as I expect.

          The reason I expect them is that when testing using the online tools (using various RegEx "flavours"), I get 3 captures. Here's a link showing just that:

          https://regex101.com/r/ZlzWNM/3

          If I then try the same pattern and text in the regexp Qt example project, I only get 1 capture.

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

          @webzoid
          I don't know what the code of "regexp Qt example project" does, one way or the other. You'd have to look at it to see what it's doing & reporting.

          However, can't you change over the recommended QRegularExpressionclass I mentioned? Since that states it's Perl-compatible, you'll know its behaviour is as per when you have looked at Perl flavor regular expressions.

          webzoidW 1 Reply Last reply
          1
          • JonBJ JonB

            @webzoid
            I don't know what the code of "regexp Qt example project" does, one way or the other. You'd have to look at it to see what it's doing & reporting.

            However, can't you change over the recommended QRegularExpressionclass I mentioned? Since that states it's Perl-compatible, you'll know its behaviour is as per when you have looked at Perl flavor regular expressions.

            webzoidW Offline
            webzoidW Offline
            webzoid
            wrote on last edited by
            #5

            @JonB I'll move my code over to the QRegularExpression class as you've mentioned.

            JonBJ 1 Reply Last reply
            0
            • webzoidW webzoid

              @JonB I'll move my code over to the QRegularExpression class as you've mentioned.

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

              @webzoid
              It's probably the best idea anyway. Then hopefully you will see your pattern work exactly as per Perl, which you've probably checked in your regular expression tester as doing what you want.

              webzoidW 1 Reply Last reply
              1
              • JonBJ JonB

                @webzoid
                It's probably the best idea anyway. Then hopefully you will see your pattern work exactly as per Perl, which you've probably checked in your regular expression tester as doing what you want.

                webzoidW Offline
                webzoidW Offline
                webzoid
                wrote on last edited by
                #7

                @JonB Works like a treat, thanks for your help. I shall no longer use QRegExp. Thanks again

                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