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
QtWS25 Last Chance

Regex doesn't seem to work with Qt

Scheduled Pinned Locked Moved Solved General and Desktop
qregexp
15 Posts 5 Posters 1.3k 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.
  • JonBJ JonB

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

    Petross404_Petros SP Offline
    Petross404_Petros SP Offline
    Petross404_Petros S
    wrote on 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
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on 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

      Petross404_Petros SP 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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

        Petross404_Petros SP 1 Reply Last reply
        0
        • VRoninV VRonin

          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);
          }
          
          Petross404_Petros SP Offline
          Petross404_Petros SP Offline
          Petross404_Petros S
          wrote on 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.

          VRoninV 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            Are you trying to use file glob pattern ?

            Petross404_Petros SP Offline
            Petross404_Petros SP Offline
            Petross404_Petros S
            wrote on 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
            • Petross404_Petros SP Petross404_Petros S

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

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on 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

              Petross404_Petros SP 1 Reply Last reply
              1
              • VRoninV VRonin

                @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

                Petross404_Petros SP Offline
                Petross404_Petros SP Offline
                Petross404_Petros S
                wrote on 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
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on 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
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 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

                    Petross404_Petros SP 1 Reply Last reply
                    3
                    • SGaistS SGaist

                      QRegularExpression::wildcardToRegularExpression is what you were looking for.

                      Petross404_Petros SP Offline
                      Petross404_Petros SP Offline
                      Petross404_Petros S
                      wrote on 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

                      • Login

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