How to retrieve / match - at least next "pair"? (regular expression )
-
wrote on 10 Nov 2023, 01:26 last edited by
Here is my command result ,
I can "match" hcix with address , but only one pair.
Here is my reg expressions usageQRegularExpression 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"
-
Here is my command result ,
I can "match" hcix with address , but only one pair.
Here is my reg expressions usageQRegularExpression 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"
wrote on 10 Nov 2023, 09:08 last edited by@AnneRanch
If you are usingQRegularExpression::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. -
@AnneRanch
If you are usingQRegularExpression::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.wrote on 10 Nov 2023, 18:20 last edited by@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 timeI 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;
-
@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 timeI 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;
wrote on 10 Nov 2023, 18:25 last edited by JonB 11 Oct 2023, 18:26@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 usingQRegularExpression::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. -
-
@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 timeI 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;
wrote on 14 Nov 2023, 08:56 last edited by@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
5/5