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. Regex doesn't seem to work with Qt
Forum Updated to NodeBB v4.3 + New Features

Regex doesn't seem to work with Qt

Scheduled Pinned Locked Moved Solved General and Desktop
qregexp
15 Posts 5 Posters 1.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.
  • A artwaw
    15 Jun 2021, 16:03

    @Petross404_Petros-S QRegExp is obsolete, please use QRegularExpression.

    P Offline
    P Offline
    Petross404_Petros S
    wrote on 15 Jun 2021, 16:17 last edited by Petross404_Petros S
    #3

    @artwaw said in Regex doesn't seem to work with Qt:

    @Petross404_Petros-S QRegExp is obsolete, please use QRegularExpression.

    You are right, but since this a contribution to a project that isn't mine, the developer needs the the minimum Qt version to be Qt5.4

    Am I on the safe side?

    A 1 Reply Last reply 15 Jun 2021, 16:18
    0
    • P Petross404_Petros S
      15 Jun 2021, 16:17

      @artwaw said in Regex doesn't seem to work with Qt:

      @Petross404_Petros-S QRegExp is obsolete, please use QRegularExpression.

      You are right, but since this a contribution to a project that isn't mine, the developer needs the the minimum Qt version to be Qt5.4

      Am I on the safe side?

      A Offline
      A Offline
      artwaw
      wrote on 15 Jun 2021, 16:18 last edited by
      #4

      @Petross404_Petros-S In the documentation to QRegularExpression class documentation there is clearly written This class was introduced in Qt 5.0.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      0
      • P Petross404_Petros S
        15 Jun 2021, 16:00

        Hello guys, I hope you are doing well. I am trying to create a regex in order to filter some text:

        Symbols from "/mnt/home/petros/test".
        Local exec file:
                `/mnt/home/petros/test', file type elf64-x86-64.
                Entry point: 0x1050
                0x0000000000000318 - 0x0000000000000334 is .interp
                0x0000000000000338 - 0x0000000000000368 is .note.gnu.property
                0x0000000000000368 - 0x0000000000000388 is .note.ABI-tag
                0x0000000000000388 - 0x00000000000003ac is .gnu.hash
                0x00000000000003b0 - 0x0000000000000458 is .dynsym
                0x0000000000000458 - 0x00000000000004da is .dynstr
                0x00000000000004da - 0x00000000000004e8 is .gnu.version
                0x00000000000004e8 - 0x0000000000000508 is .gnu.version_r
                0x0000000000000508 - 0x00000000000005c8 is .rela.dyn
                0x00000000000005c8 - 0x00000000000005e0 is .rela.plt
                0x0000000000001000 - 0x0000000000001017 is .init
                0x0000000000001020 - 0x0000000000001040 is .plt
                0x0000000000001040 - 0x0000000000001048 is .plt.got
                0x0000000000001050 - 0x00000000000011d9 is .text
                0x00000000000011dc - 0x00000000000011e5 is .fini
                0x0000000000002000 - 0x0000000000002017 is .rodata
                0x0000000000002018 - 0x0000000000002054 is .eh_frame_hdr
                0x0000000000002058 - 0x0000000000002144 is .eh_frame
                0x0000000000003db8 - 0x0000000000003dc0 is .init_array
                0x0000000000003dc0 - 0x0000000000003dc8 is .fini_array
                0x0000000000003dc8 - 0x0000000000003fb8 is .dynamic
                0x0000000000003fb8 - 0x0000000000004000 is .got
                0x0000000000004000 - 0x0000000000004010 is .data
                0x0000000000004010 - 0x0000000000004018 is .bss
        

        This regex here (/file type.*/g) filters correctly what I am trying to do. Basically I will delete the "file type " part later and keep only the "elf64-x86-64." but that's ok.

        When I try to create this regex with Qt though it doesn't seem to work:

            QRegExp re{"//file type.*//g"};    // Even without escaping it, it doesn't capture anything.
            QString str{output};               //This is the wall of text above.
            
        
            qDebug() << re.capturedTexts();                    // ("")
            qDebug() << "exact match? " << re.exactMatch(str); // exact match?  false
        

        So my question is does Qt5 have a different syntax for regular expressions or am I missing something else here?

        J Offline
        J Offline
        JonB
        wrote on 15 Jun 2021, 16:41 last edited by JonB
        #5

        @Petross404_Petros-S said in Regex doesn't seem to work with Qt:

        QRegExp re{"//file type.*//g"}; // Even without escaping it, it doesn't capture anything.

        Given you're using QRegExp (wouldn't make any difference if QRegularExpression anyway). Where did you get the idea this is suitable input for it? It is not.

        So my question is does Qt5 have a different syntax for regular expressions or am I missing something else here?

        This regex here (/file type.*/g)

        You seem to be assuming that what you see on that page you reference is literally what you can use elsewhere. Though even then you have changed it. And you seem to have selected for JavaScript.

        P 1 Reply Last reply 15 Jun 2021, 18:20
        0
        • J JonB
          15 Jun 2021, 16:41

          @Petross404_Petros-S said in Regex doesn't seem to work with Qt:

          QRegExp re{"//file type.*//g"}; // Even without escaping it, it doesn't capture anything.

          Given you're using QRegExp (wouldn't make any difference if QRegularExpression anyway). Where did you get the idea this is suitable input for it? It is not.

          So my question is does Qt5 have a different syntax for regular expressions or am I missing something else here?

          This regex here (/file type.*/g)

          You seem to be assuming that what you see on that page you reference is literally what you can use elsewhere. Though even then you have changed it. And you seem to have selected for JavaScript.

          P Offline
          P Offline
          Petross404_Petros S
          wrote on 15 Jun 2021, 18:20 last edited by
          #6

          @JonB said in Regex doesn't seem to work with Qt:

          @Petross404_Petros-S said in Regex doesn't seem to work with Qt:

          QRegExp re{"//file type.*//g"}; // Even without escaping it, it doesn't capture anything.

          Given you're using QRegExp (wouldn't make any difference if QRegularExpression anyway). Where did you get the idea this is suitable input for it? It is not.

          So my question is does Qt5 have a different syntax for regular expressions or am I missing something else here?

          This regex here (/file type.*/g)

          You seem to be assuming that what you see on that page you reference is literally what you can use elsewhere. Though even then you have changed it. And you seem to have selected for JavaScript.

          Oh sorry then, how could I miss this?

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 15 Jun 2021, 18:34 last edited by VRonin
            #7

            the /g is not how you set the regex to global

            const QRegularExpression regExp(QStringLiteral(R"***(file type.*)***"));
            QRegularExpressionMatchIterator regExpMatch =  regExp.globalMatch(input);
            while (regExpMatch .hasNext()) {
                const QRegularExpressionMatch match = regExpMatch .next();
                qDebug() <<  match.capturedRef(0);
            }
            

            "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

            P 1 Reply Last reply 16 Jun 2021, 15:37
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 15 Jun 2021, 18:58 last edited by
              #8

              Hi,

              Are you trying to use file glob pattern ?

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

              P 1 Reply Last reply 16 Jun 2021, 15:39
              0
              • V VRonin
                15 Jun 2021, 18:34

                the /g is not how you set the regex to global

                const QRegularExpression regExp(QStringLiteral(R"***(file type.*)***"));
                QRegularExpressionMatchIterator regExpMatch =  regExp.globalMatch(input);
                while (regExpMatch .hasNext()) {
                    const QRegularExpressionMatch match = regExpMatch .next();
                    qDebug() <<  match.capturedRef(0);
                }
                
                P Offline
                P Offline
                Petross404_Petros S
                wrote on 16 Jun 2021, 15:37 last edited by
                #9

                @VRonin said in Regex doesn't seem to work with Qt:

                the /g is not how you set the regex to global

                const QRegularExpression regExp(QStringLiteral(R"***(file type.*)***"));
                QRegularExpressionMatchIterator regExpMatch =  regExp.globalMatch(input);
                while (regExpMatch .hasNext()) {
                    const QRegularExpressionMatch match = regExpMatch .next();
                    qDebug() <<  match.capturedRef(0);
                }
                

                Yes, it seems that it needed a different approach. This works:

                QString str{output};  //The output from the first post.
                
                const QRegularExpression regExp(QStringLiteral(R"***(file type.*)***"));
                QRegularExpressionMatch regExpMatch =  regExp.match(str);
                
                QString strFileType {regExpMatch.captured(0)};   //"file type elf64-x86-64."
                
                if(strFileType.contains("file type") && strFileType.contains("."))
                {
                    strFileType.replace("file type ", "");
                    strFileType.replace(".", "");
                }
                
                That was a little
                qDebug() << strFileType;                       // "elf64-x86-64"
                

                Thank you all.

                V 1 Reply Last reply 16 Jun 2021, 15:49
                0
                • S SGaist
                  15 Jun 2021, 18:58

                  Hi,

                  Are you trying to use file glob pattern ?

                  P Offline
                  P Offline
                  Petross404_Petros S
                  wrote on 16 Jun 2021, 15:39 last edited by
                  #10

                  @SGaist said in Regex doesn't seem to work with Qt:

                  Hi,

                  Are you trying to use file glob pattern ?

                  Yes but in the wrong way. Thank you for your interest.

                  1 Reply Last reply
                  0
                  • P Petross404_Petros S
                    16 Jun 2021, 15:37

                    @VRonin said in Regex doesn't seem to work with Qt:

                    the /g is not how you set the regex to global

                    const QRegularExpression regExp(QStringLiteral(R"***(file type.*)***"));
                    QRegularExpressionMatchIterator regExpMatch =  regExp.globalMatch(input);
                    while (regExpMatch .hasNext()) {
                        const QRegularExpressionMatch match = regExpMatch .next();
                        qDebug() <<  match.capturedRef(0);
                    }
                    

                    Yes, it seems that it needed a different approach. This works:

                    QString str{output};  //The output from the first post.
                    
                    const QRegularExpression regExp(QStringLiteral(R"***(file type.*)***"));
                    QRegularExpressionMatch regExpMatch =  regExp.match(str);
                    
                    QString strFileType {regExpMatch.captured(0)};   //"file type elf64-x86-64."
                    
                    if(strFileType.contains("file type") && strFileType.contains("."))
                    {
                        strFileType.replace("file type ", "");
                        strFileType.replace(".", "");
                    }
                    
                    That was a little
                    qDebug() << strFileType;                       // "elf64-x86-64"
                    

                    Thank you all.

                    V Offline
                    V Offline
                    VRonin
                    wrote on 16 Jun 2021, 15:49 last edited by
                    #11

                    @Petross404_Petros-S said in Regex doesn't seem to work with Qt:

                    strFileType.contains(".")

                    . in regexp has a special menaing. if you want to match the literal dot you have to escape it

                    "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

                    P 1 Reply Last reply 16 Jun 2021, 16:27
                    1
                    • V VRonin
                      16 Jun 2021, 15:49

                      @Petross404_Petros-S said in Regex doesn't seem to work with Qt:

                      strFileType.contains(".")

                      . in regexp has a special menaing. if you want to match the literal dot you have to escape it

                      P Offline
                      P Offline
                      Petross404_Petros S
                      wrote on 16 Jun 2021, 16:27 last edited by Petross404_Petros S
                      #12

                      @VRonin said in Regex doesn't seem to work with Qt:

                      @Petross404_Petros-S said in Regex doesn't seem to work with Qt:

                      strFileType.contains(".")

                      . in regexp has a special menaing. if you want to match the literal dot you have to escape it

                      Interesting because it actually removed the dot. When I used it like " \. " it didn't work.
                      EDIT It doesn't show the \ characters I used above.

                      1 Reply Last reply
                      0
                      • V Offline
                        V Offline
                        VRonin
                        wrote on 16 Jun 2021, 16:33 last edited by
                        #13

                        My bad, I wasn't clear. . in QRegularExpression mean any character.

                        strFileType.replace(".", ""); is correct but unnecessary:

                        QString str{output};  //The output from the first post.
                        
                        const QRegularExpression regExp(QStringLiteral(R"***(file type\s*(.*)\.)***"));
                        QRegularExpressionMatch regExpMatch =  regExp.match(str);
                        qDebug() << regExpMatch.captured(0);   //"file type elf64-x86-64."
                        QString strFileType = regExpMatch.captured(1);
                        qDebug() << strFileType; //"elf64-x86-64"
                        

                        "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
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 16 Jun 2021, 17:01 last edited by
                          #14

                          QRegularExpression::wildcardToRegularExpression is what you were looking for.

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

                          P 1 Reply Last reply 17 Jun 2021, 11:17
                          3
                          • S SGaist
                            16 Jun 2021, 17:01

                            QRegularExpression::wildcardToRegularExpression is what you were looking for.

                            P Offline
                            P Offline
                            Petross404_Petros S
                            wrote on 17 Jun 2021, 11:17 last edited by
                            #15

                            @SGaist said in Regex doesn't seem to work with Qt:

                            QRegularExpression::wildcardToRegularExpression is what you were looking for.

                            I am afraid I can't use this function since it is very recent and compatiblity with Qt5.4 is needed. But thank you.

                            @VRonin thank you too, your example was to the point. I will take a deeper look at what you did there. Thanks again.

                            1 Reply Last reply
                            1

                            12/15

                            16 Jun 2021, 16:27

                            • Login

                            • Login or register to search.
                            12 out of 15
                            • First post
                              12/15
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved