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 return nothing with QRegularExpression
Forum Updated to NodeBB v4.3 + New Features

Regex return nothing with QRegularExpression

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 777 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.
  • D Offline
    D Offline
    Drdgekil
    wrote on last edited by aha_1980
    #1

    hi everyone,

    I have created a desktop application with QT 5.13.2 and MSVC 2017 32 bit
    It's an electronic stock manager with mongoDB in background

    I'm doing the search engine for my app.
    I've a search Field, where i can use some basic syntax.

    eg:

    Search with value only:

    • value
    • value,value,value;
    • value|value|value;

    Search with referenced value

    • ref=value
    • ref=value&ref=value&ref=value;
    • ref=value|ref=value|ref=value;

    First i planned to implement a parser, but i thinks it's a bit oversized for this purpose. So i'm gonna using regex, but i never doing that before.

    I have doing on regex101.com my regex that seem do be fonctional on the site, but when i run it with QRegularExpression they don't return the same thing.

    They are in PCRE .

    reference value = ([\w]{1,}[\s]{0,})([\=|\<|\>])([\s]{0,}[\w]{1,})([\s]{0,}[\&|\||\<|\>\;])
    value only = ([\w]{1,}[\,\h\;\|\|]{1})
    

    actually for the two regex, if i input :
    value
    OR
    ref=value
    I need to add an semi colon a the end of text. how i could rig of that ?

    And if the string is :
    element=value&element=value&element=value;

    and in debugger, i watch the QStringList the only thing that comes in is: "element"

    could you please help me for this regex problem ?

    My c++ code is: (I'm testing only referenced value here)

       const QString regexByRef = "([\\w]{1,}[\\s]{0,})([\\=|\\<|\\>])([\\s]{0,}[\\w]{1,})([\\s]{0,}[\\&|\\||\\<|\\>\\;])";
       const QString regexByValue = "([\\w]{1,}[\\,\\h\\;\\|\\&]{1})";
       QRegularExpression re(regexByRef,QRegularExpression::CaseInsensitiveOption);
       re.setPatternOptions(QRegularExpression::MultilineOption);
       if(re.isValid())
       {
           QRegularExpressionMatchIterator it = re.globalMatch(input);
           QStringList words;
           while (it.hasNext()) {
               QRegularExpressionMatch match = it.next();
               QString word = match.captured(1);
               words << word;
           }
    
       }
    
    VRoninV 1 Reply Last reply
    0
    • D Drdgekil

      hi everyone,

      I have created a desktop application with QT 5.13.2 and MSVC 2017 32 bit
      It's an electronic stock manager with mongoDB in background

      I'm doing the search engine for my app.
      I've a search Field, where i can use some basic syntax.

      eg:

      Search with value only:

      • value
      • value,value,value;
      • value|value|value;

      Search with referenced value

      • ref=value
      • ref=value&ref=value&ref=value;
      • ref=value|ref=value|ref=value;

      First i planned to implement a parser, but i thinks it's a bit oversized for this purpose. So i'm gonna using regex, but i never doing that before.

      I have doing on regex101.com my regex that seem do be fonctional on the site, but when i run it with QRegularExpression they don't return the same thing.

      They are in PCRE .

      reference value = ([\w]{1,}[\s]{0,})([\=|\<|\>])([\s]{0,}[\w]{1,})([\s]{0,}[\&|\||\<|\>\;])
      value only = ([\w]{1,}[\,\h\;\|\|]{1})
      

      actually for the two regex, if i input :
      value
      OR
      ref=value
      I need to add an semi colon a the end of text. how i could rig of that ?

      And if the string is :
      element=value&element=value&element=value;

      and in debugger, i watch the QStringList the only thing that comes in is: "element"

      could you please help me for this regex problem ?

      My c++ code is: (I'm testing only referenced value here)

         const QString regexByRef = "([\\w]{1,}[\\s]{0,})([\\=|\\<|\\>])([\\s]{0,}[\\w]{1,})([\\s]{0,}[\\&|\\||\\<|\\>\\;])";
         const QString regexByValue = "([\\w]{1,}[\\,\\h\\;\\|\\&]{1})";
         QRegularExpression re(regexByRef,QRegularExpression::CaseInsensitiveOption);
         re.setPatternOptions(QRegularExpression::MultilineOption);
         if(re.isValid())
         {
             QRegularExpressionMatchIterator it = re.globalMatch(input);
             QStringList words;
             while (it.hasNext()) {
                 QRegularExpressionMatch match = it.next();
                 QString word = match.captured(1);
                 words << word;
             }
      
         }
      
      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2
      const QRegularExpression re(QStringLiteral(R"***((?:[\,\;\h\|]?(\w+))|(?:([\&\|\<\>]?)(\w+)\s*=\s*(\w+)\s*\;?))***"));
      QRegularExpressionMatchIterator it = re.globalMatch(input);
      while (it.hasNext()) {
      const QRegularExpressionMatch match = it.next();
      switch(match.lastCapturedIndex()){
      case 1: //value only
      qDebug() << "Captured Value: " << match.capturedRef(1);
      break;
      case 2: // first referenced value
      qDebug() << match.capturedRef(1) << "=" << match.capturedRef(2);
      break;
      case 3: //subsequent referenced value
      qDebug() << "Separator: " << match.capturedRef(1);
      qDebug() << match.capturedRef(2) << "=" << match.capturedRef(3);
      break;
      default:
      Q_UNREACHABLE();
      break;
      }
      }
      

      "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
      4
      • D Offline
        D Offline
        Drdgekil
        wrote on last edited by Drdgekil
        #3

        Hi @VRonin
        Thanks for your response,

        i've tried your code.
        if i input:

        element=value&element=value&element=value;
        

        it returns me :

        element 
        value
        

        but i need the separator everytime, plus it gave me only the first occurence of

        element=value
        

        after that it raise the exception Q_UNREACHABLE

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

          ok, this is tested and should be working:

          #include <QRegularExpression>
          #include <QDebug>
          int main(int argc, char *argv[])
          {
              const QString inputs[] = {
                  QStringLiteral("value")
                  ,QStringLiteral("value,value,value;")
                  ,QStringLiteral("value|value|value;")
                  ,QStringLiteral("ref=value")
                  ,QStringLiteral("ref=value&ref=value&ref=value;")
                  ,QStringLiteral("ref=value|ref=value|ref=value;")
                  ,QStringLiteral("element=value&element=value&element=value;")
              };
              const QRegularExpression re(QStringLiteral(R"***(([\&\|\<\>\h,]?)(\w+)\s*(?:=\s*(\w+)\s*\;?)?)***"));
              for(const QString& input : inputs){
                  qDebug() << "Input: " << input;
                  QRegularExpressionMatchIterator it = re.globalMatch(input);
                  while (it.hasNext()) {
                      const QRegularExpressionMatch match = it.next();
                      switch(match.lastCapturedIndex()){
                      case 2:
                          qDebug() << "Value Only";
                          if(!match.capturedRef(1).isEmpty())
                              qDebug() << "Previous separator: " <<  match.capturedRef(1);
                          qDebug() << "Value: " <<  match.capturedRef(2);
                          break;
                      case 3:
                          qDebug() << "Referenced Value";
                          if(!match.capturedRef(1).isEmpty())
                              qDebug() << "Previous separator: " <<  match.capturedRef(1);
                          qDebug() << "Value: " <<  match.capturedRef(2) << "=" << match.capturedRef(3);
                          break;
                      default:
                          Q_UNREACHABLE();
                          break;
                      }
                  }
              }
              return 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

          1 Reply Last reply
          5
          • D Offline
            D Offline
            Drdgekil
            wrote on last edited by
            #5

            Hi,
            thanks it's working like a charm.

            what is the utility of the R"*** ?

            VRoninV 1 Reply Last reply
            0
            • D Drdgekil

              Hi,
              thanks it's working like a charm.

              what is the utility of the R"*** ?

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @Drdgekil said in Regex return nothing with QRegularExpression:

              what is the utility of the R"*** ?

              It's a raw string literal, it allows you not to care about escaping characters in the string

              "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
              2
              • D Offline
                D Offline
                Drdgekil
                wrote on last edited by
                #7

                thanks for your help, it's very appreciate

                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