Qt World Summit: Register Today!

QRegExp Global Flag

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

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

  • @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:


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

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

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

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

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

Log in to reply