Qt Regular expresssion example - how to use it?
-
https://doc.qt.io/qt-6/qtwidgets-tools-regularexpression-example.html
I have not been able to guess how to actually use this Qt published example.
I can guess how to "paste" the text to analyze...The rest of it , what to enter what to "match " , is not that intuitive...
Please help if you actually used this Qt example.
-
@AnneRanch said in Qt Regular expresssion example - how to use it?:
I have not been able to guess how to actually use this Qt published example.
It helps a lot to know RegEx before playing around with the example ;-)
The rest of it , what to enter what to "match " , is not that intuitive...
What do you expect?!
You type in your RegEx and a text to apply it on. The result/matches (if any) are shown on the right.RegEx tutorial with exercises:
Here is a RegEx online test tool:
Here one with more detailed explanations
-
@Pl45m4 OK, I "paste" the "text to mach" ... then what ?
(My options are "solution " or "continue - which is actually start next "match" task )
I do not see any "results on the right "...How do I actually enter the "match" ? in my case "hci" - that is the part I need help with..
I expect "cut and paste" - I can highlight t6her desired match in the entered text and THEN WHAT ?
Some reg expression generators "automatically " apply the "match " ....PS I am also using QT regexp example - which is typical - no comments in source code and no "F1" help either.
-
@AnneRanch
In your image you are applying a random and complicated regex to the given exercise, which is intended to learn how this stuff works...
To complete Exercise 1 you type in a regex which matches all three required matches below- abcdefg
- abcde
- abc
Sigh, I really dont know why you chose to pick the website with the exercises out of all the RegEx pages I linked in my previous post, to test your own regex....
Go to
paste your regex and your text and check the result.
RegEx'es in general might seem somewhat complicated, but using these websites to check an existing one on a text isn't rocket science...
The same applies to the Qt example.Btw: If you would have posted your regex as code/text and not as screenshot, others can copy it and test it as well...
And if you need help with creating your RegEx, if would help to know what your text looks like and what parts you want to match.
-
In my view it is irrelevant IF all links with reg exp generators work BUT
this does not//QRegularExpression re("hci[0-9]"); TOK
//QRegularExpression re("hci[0-9]/g"); //no match
//QRegularExpression re("^hci[0-9]/g$"); //no match// QRegularExpression re("hci[0-9]//gm"); //no match
I have been getting responses with links to AI reg exp generators AFTER I have REPEATEDLY asked NOT to keep posting these links.
I have also stated many times I need help with retrieving multiple matches...
I have posted the "text" many times too...Enough trying to make me feel qulty that I am not posting my code...
PLEASE
help me to find why most AI generators expression works as EXPECTED AND same reg expression won't.
Unless somebody can actually demonstrated WHY
QRegularExpression expressions for multiple matches DOES NOT WORK
my next step will be to looks for other tools which will do what is expected.Here is the text I want to extract multiple "hcix" from :
"Devices:\n\thci1\t00:50:B6:80:4D:5D\n\thci0\t00:15:83:15:A2:CB\n"You are free, your choice, to "cut and paste" it to tool of your choice.
preferably Qt C++
but you can do "visual basic" if so desired ... ( sarcasm ) -
@AnneRanch
I'm not commenting on the other stuff, but how about looking for
"RegEx match MAC (or hardware address)"
I assume you only want to extract
00:50:B6:80:4D:5D
and00:15:83:15:A2:CB
then you could use something like
([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})
Test here:
-
Unfortunately that is NOT the issue
the issues is how to do this - in pseudo codehci[0-9] { 2} - run hci[0-9] twice
this does not work using Qt RegExp but it works fine
elsewhere as
hci[0-9] /gm -
@AnneRanch said in Qt Regular expresssion example - how to use it?:
hci[0-9] { 2} - run hci[0-9] twice
Do you want to count the hci devices?! So you dont even need the MAC addresses? Only
hci0
andhci1
Isnt there a better way for this? -
@Pl45m4 My current goal is to actually connect Bluetooth adapters to test passing serial data between them. After that is working I will replace the "secondary " adapter with my remote device. Partially because I do not have (temporary - no power outlet ) physical access to the remote device,
I do have "results" of Bluetooth command and I just need to "split it " to name and address. . I can do that using AI generators but cannot figure out why Qt won't work same way . -
@AnneRanch said in Qt Regular expresssion example - how to use it?:
QRegularExpression expressions for multiple matches DOES NOT WORK
As answered in your https://forum.qt.io/topic/152003/how-to-retrieve-match-at-least-next-pair-regular-expression, for your case you must use
QRegularExpression::globalMatch()
instead ofQRegularExpression::match()
. That is what you are looking for in your case. Like the following:QRegularExpression re("hci[0-9]+\t[0-9A-F:]+"); QRegularExpressionMatchIterator i = re.globalMatch(yourVariableOfTheInputString); while (i.hasNext()) { QRegularExpressionMatch match = i.next(); qDebug() << match.captured(); }
So from your input string in that other post, complete program:
#include <QCoreApplication> #include <QDebug> #include <QRegularExpression> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString input ( "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" ); QRegularExpression re("hci[0-9]+\t[0-9A-F:]+"); QRegularExpressionMatchIterator i = re.globalMatch(input); while (i.hasNext()) { QRegularExpressionMatch match = i.next(); qDebug() << match.captured(); } return a.exec(); }
produces output
"hci8\t00:50:B6:80:4D:5D" "hci0\t00:15:83:15:A2:CB" "hci8\t00:50:B6:80:4D:5D" "hci0\t00:15:83:15:A2:CB" "hci8\t00:50:B6:80:4D:5D" "hci0\t00:15:83:15:A2:CB" "hci8\t00:50:B6:80:4D:5D" "hci8\t00:50:B6:80:4D:5D"
which is just what you wanted, right?
-