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 for String in quotes

QRegExp for String in quotes

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 6.4k Views 2 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    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.

    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
    1
    • SikarjanS Offline
      SikarjanS Offline
      Sikarjan
      wrote on last edited by
      #3

      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 =(

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

        Do you mean the syntax highlighter example ?

        You also have the regular expression tool for QRegExp.

        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
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #5

          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 ("")

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          SikarjanS 1 Reply Last reply
          1
          • SikarjanS Offline
            SikarjanS Offline
            Sikarjan
            wrote on last edited by
            #6

            Hi all,

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

            private:
                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.

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #7

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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              0
              • SikarjanS Offline
                SikarjanS Offline
                Sikarjan
                wrote on last edited by
                #8

                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.

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

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

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

                  SikarjanS 1 Reply Last reply
                  1
                  • VRoninV VRonin

                    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 ("")

                    SikarjanS Offline
                    SikarjanS Offline
                    Sikarjan
                    wrote on last edited by
                    #10

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

                    1 Reply Last reply
                    0
                    • VRoninV Offline
                      VRoninV Offline
                      VRonin
                      wrote on last edited by
                      #11

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

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      1 Reply Last reply
                      0
                      • SikarjanS Offline
                        SikarjanS Offline
                        Sikarjan
                        wrote on last edited by
                        #12

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

                        Thanks

                        1 Reply Last reply
                        0
                        • VRoninV Offline
                          VRoninV Offline
                          VRonin
                          wrote on last edited by
                          #13

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

                          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                          ~Napoleon Bonaparte

                          On a crusade to banish setIndexWidget() from the holy land of Qt

                          1 Reply Last reply
                          0
                          • SGaistS SGaist

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

                            SikarjanS Offline
                            SikarjanS Offline
                            Sikarjan
                            wrote on last edited by
                            #14

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

                            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