Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to retrieve / match - at least next "pair"? (regular expression )

How to retrieve / match - at least next "pair"? (regular expression )

Scheduled Pinned Locked Moved Unsolved C++ Gurus
5 Posts 2 Posters 499 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.
  • A Offline
    A Offline
    Anonymous_Banned275
    wrote on last edited by
    #1

    Here is my command result ,

    I can "match" hcix with address , but only one pair.
    Here is my reg expressions usage

    QRegularExpression re("[hci[0-9]+\t[0-9A-F:]+]*");
    QRegularExpression re("[hci[0-9]+\t[0-9A-F:]+]+");

    both code matches only the first hcix / address

    how do I modify the reg expression to match BOTH pairs?

    At present I have only two hci devices / adapters but I will need more in future.

    I did try AI reg expression generator but it did now work, several of them.

    I basically need a syntax to [match hcix with its address ]
    and then REPEAT for rest of the pairs - hcix and its address.

    "Devices:\n\thci8\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n"
    "Devices:\n\thci8\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n"
    " SUCCESS test_contains Devices:Devices:\n\thci8\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n DEBUG TASK START Bluetooth "
    " MATCH result "
    "hci8\t00:50:B6:80:4D:5D"
    " MATCH result "
    " SCAN results hci8\t00:50:B6:80:4D:5D"

    only the first pair is "matched"

    JonBJ 1 Reply Last reply
    0
    • A Anonymous_Banned275

      Here is my command result ,

      I can "match" hcix with address , but only one pair.
      Here is my reg expressions usage

      QRegularExpression re("[hci[0-9]+\t[0-9A-F:]+]*");
      QRegularExpression re("[hci[0-9]+\t[0-9A-F:]+]+");

      both code matches only the first hcix / address

      how do I modify the reg expression to match BOTH pairs?

      At present I have only two hci devices / adapters but I will need more in future.

      I did try AI reg expression generator but it did now work, several of them.

      I basically need a syntax to [match hcix with its address ]
      and then REPEAT for rest of the pairs - hcix and its address.

      "Devices:\n\thci8\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n"
      "Devices:\n\thci8\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n"
      " SUCCESS test_contains Devices:Devices:\n\thci8\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n DEBUG TASK START Bluetooth "
      " MATCH result "
      "hci8\t00:50:B6:80:4D:5D"
      " MATCH result "
      " SCAN results hci8\t00:50:B6:80:4D:5D"

      only the first pair is "matched"

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @AnneRanch
      If you are using QRegularExpression::match() that will match and return only the first match. To keep matching you need to use QRegularExpressionMatchIterator QRegularExpression::globalMatch(). That returns a QRegularExpressionMatchIterator, on which you iterate to return each of the matches, until it returns no more. Example code is given both in that topic and in Global Matching.

      A 1 Reply Last reply
      2
      • JonBJ JonB

        @AnneRanch
        If you are using QRegularExpression::match() that will match and return only the first match. To keep matching you need to use QRegularExpressionMatchIterator QRegularExpression::globalMatch(). That returns a QRegularExpressionMatchIterator, on which you iterate to return each of the matches, until it returns no more. Example code is given both in that topic and in Global Matching.

        A Offline
        A Offline
        Anonymous_Banned275
        wrote on last edited by
        #3

        @JonB Are you saying my iterator is the problem ?
        I believe the "+"
        [0-9A-F:]+

        instruct the process to run multiple [0-9A-F:] "matches" ,
        so I am under the impression I should be able to
        instruct to run the entire
        [hci[0-9]+\t[0-9A-F:]+]+
        by adding the last "+" - multiple time

        I am looking at this as - the "match" is the issue , not the iteration.
        or
        if there is no match , the iteration WILL have no choice but to fail .

               QRegularExpression re("[hci[0-9]+\\t[0-9A-F:]+]*");
        
                //QRegularExpression re("[0-9A-F:]+"); // picks from SUCCESS ? CCE
                QRegularExpressionMatch match = re.match(text);
        
                if (match.hasMatch()) { // matches all
                    //text = " Has all match ";
                    QStringList result = match.capturedTexts();
                    text = " MATCH result ";
                    qDebug() << text;
                    qDebug() << result.at(0);
                    text = " MATCH result ";
                    qDebug() << text;
                    QString item;
                    foreach(item,result)
                    {
                        text = " SCAN  results ";
                        text += item;
                        qDebug() << text;
                    }
                }
         qDebug() << text;
        
        JonBJ 2 Replies Last reply
        0
        • A Anonymous_Banned275

          @JonB Are you saying my iterator is the problem ?
          I believe the "+"
          [0-9A-F:]+

          instruct the process to run multiple [0-9A-F:] "matches" ,
          so I am under the impression I should be able to
          instruct to run the entire
          [hci[0-9]+\t[0-9A-F:]+]+
          by adding the last "+" - multiple time

          I am looking at this as - the "match" is the issue , not the iteration.
          or
          if there is no match , the iteration WILL have no choice but to fail .

                 QRegularExpression re("[hci[0-9]+\\t[0-9A-F:]+]*");
          
                  //QRegularExpression re("[0-9A-F:]+"); // picks from SUCCESS ? CCE
                  QRegularExpressionMatch match = re.match(text);
          
                  if (match.hasMatch()) { // matches all
                      //text = " Has all match ";
                      QStringList result = match.capturedTexts();
                      text = " MATCH result ";
                      qDebug() << text;
                      qDebug() << result.at(0);
                      text = " MATCH result ";
                      qDebug() << text;
                      QString item;
                      foreach(item,result)
                      {
                          text = " SCAN  results ";
                          text += item;
                          qDebug() << text;
                      }
                  }
           qDebug() << text;
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @AnneRanch said in How to retrieve / match - at least next "pair"? (regular expression ):

          QRegularExpressionMatch match = re.match(text);

          You are using QRegularExpression::match(), which returns the first match only. As I said, you need to change over to using QRegularExpression::globalMatch() to access all the matches of your regular expression in your input string, which is what you are looking for,. The example code at https://doc.qt.io/qt-6/qregularexpression.html#global-matching shows the code you need.

          1 Reply Last reply
          0
          • JonBJ JonB referenced this topic on
          • A Anonymous_Banned275

            @JonB Are you saying my iterator is the problem ?
            I believe the "+"
            [0-9A-F:]+

            instruct the process to run multiple [0-9A-F:] "matches" ,
            so I am under the impression I should be able to
            instruct to run the entire
            [hci[0-9]+\t[0-9A-F:]+]+
            by adding the last "+" - multiple time

            I am looking at this as - the "match" is the issue , not the iteration.
            or
            if there is no match , the iteration WILL have no choice but to fail .

                   QRegularExpression re("[hci[0-9]+\\t[0-9A-F:]+]*");
            
                    //QRegularExpression re("[0-9A-F:]+"); // picks from SUCCESS ? CCE
                    QRegularExpressionMatch match = re.match(text);
            
                    if (match.hasMatch()) { // matches all
                        //text = " Has all match ";
                        QStringList result = match.capturedTexts();
                        text = " MATCH result ";
                        qDebug() << text;
                        qDebug() << result.at(0);
                        text = " MATCH result ";
                        qDebug() << text;
                        QString item;
                        foreach(item,result)
                        {
                            text = " SCAN  results ";
                            text += item;
                            qDebug() << text;
                        }
                    }
             qDebug() << text;
            
            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @AnneRanch
            I have written the complete example code, giving what you want, at https://forum.qt.io/topic/152045/qt-regular-expresssion-example-how-to-use-it/10

            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