QRegExp for String in quotes

  • Hi,

    I am looking for the right QRegExp to detect a string within quotes. The expression should capture singe and double quotes and recognise escaped quotes. In PHP this would be the right one but it does not work in a QRegExp.



  • Lifetime Qt Champion


    Are you using Qt 4 or 5, if Qt 5 then change for QRegularExpression.

    You can also use QRegularExpression tool to test and validate your regular expressions.

  • Thanks for the answer. Is this the only way? Is it not possible to do it with QRegExp? I am working an a highlighter, from the example and going to QRegularExpression breaks my code =(

  • Lifetime Qt Champion

    Do you mean the syntax highlighter example ?

    You also have the regular expression tool for QRegExp.

  • const QRegularExpression matchQuoted(R"**((?<!\\)([\"'])(.+?)(?<!\\)\1)**");

    recognise escaped quotes

    Here I suppose that to escape a quote you use the C way (\") rather than the VBA way ("")

  • Hi all,

    thanks for the help so far. Yes, this is the example I am working with. The issue is in the header file

        struct HighlightingRule
            QRegExp pattern; // <- here
            QTextCharFormat format;

    When I change this line to QRegularExpression pattern; I get an error:

    \components\highlighter.h:80: Fehler: field 'pattern' has incomplete type 'QRegularExpression' QRegularExpression pattern;

    I have too little knowledge about C++ or Qt to understand that.

  • add #include <QRegularExpression> at the top of the file

  • Wow that is the solution. I did not think of that because Qt Creator picked it up in auto complete so I guessed it was included...

    So what I did in case someone is facing the same issue:
    In the syntax highlighter example I exchanged all QRegExp with QRegularExpression (except on the multiline thing) and included QRegularExpression in the header file. In the cpp file I changed the block function like this:

    void Highlighter::highlightBlock(const QString &text)
        foreach (const HighlightingRule &rule, highlightingRules) {
            QRegularExpression exp(rule.pattern);
            QRegularExpressionMatchIterator matches = exp.globalMatch(text);
            while (matches.hasNext()) {
                QRegularExpressionMatch match = matches.next();
                setFormat(match.capturedStart(), match.capturedLength(), rule.format);

    This works but throws a lot of warning:

    QRegularExpressionPrivate::doMatch(): called on an invalid QRegularExpression object
    So I guess the code can be optimized...

    Thanks again for the help.

  • Lifetime Qt Champion

    I have an update to the example almost ready, I'll submit it later when done.

  • @VRonin said in QRegExp for String in quotes:

    const QRegularExpression matchQuoted(R"**((?<!\\)([\"'])(.+?)(?<!\\)\1)**");

    Sorry to bother you again. How do I make this work for multiline as well?

  • const QRegularExpression matchQuoted(R"**((?<!\\)([\"'])(.+?)(?<!\\)\1)**",QRegularExpression::DotMatchesEverythingOption | QRegularExpression::MultilineOption);

  • Okay I was missing the DotMatchesEverythingOption. The line break was probably not included. Now I got it working the Simulator.


  • If you want to match even empty strings ("" or '') replace (.+?) with (.*?)

  • @SGaist said in QRegExp for String in quotes:

    I have an update to the example almost ready, I'll submit it later when done.

    I am interested to see that because I was not able to do a multiline String highlight with the expression suggested by VRonin.

    I do not really understand how this works with the blocks and everything. So probably the text variable is not containing the right section for the expression to work.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.