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. Qt Regular expresssion example - how to use it?
Forum Update on Monday, May 27th 2025

Qt Regular expresssion example - how to use it?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 1.1k 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

    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.

    Pl45m4P 1 Reply Last reply
    0
    • A Anonymous_Banned275

      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.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

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

      • https://regexone.com/

      Here is a RegEx online test tool:

      • https://regex101.com/

      Here one with more detailed explanations

      • https://regexr.com/

      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      A 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

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

        • https://regexone.com/

        Here is a RegEx online test tool:

        • https://regex101.com/

        Here one with more detailed explanations

        • https://regexr.com/
        A Offline
        A Offline
        Anonymous_Banned275
        wrote on last edited by
        #3

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

        a7967e33-1852-4747-b2e2-6dd9ea047851-image.png
        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.

        Pl45m4P 1 Reply Last reply
        0
        • A Anonymous_Banned275

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

          a7967e33-1852-4747-b2e2-6dd9ea047851-image.png
          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.

          Pl45m4P Offline
          Pl45m4P Offline
          Pl45m4
          wrote on last edited by Pl45m4
          #4

          @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

          • https://regex101.com/
            or
          • https://regexr.com/

          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.


          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

          ~E. W. Dijkstra

          1 Reply Last reply
          1
          • A Offline
            A Offline
            Anonymous_Banned275
            wrote on last edited by
            #5

            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 )

            Pl45m4P JonBJ 2 Replies Last reply
            0
            • A Anonymous_Banned275

              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 )

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #6

              @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 and 00:15:83:15:A2:CB

              then you could use something like

              ([0-9A-Fa-f]{2}[:]){5}([0-9A-Fa-f]{2})

              Test here:

              • https://regex101.com/r/4LOmpm/1

              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Anonymous_Banned275
                wrote on last edited by
                #7

                Unfortunately that is NOT the issue
                the issues is how to do this - in pseudo code

                hci[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

                Pl45m4P 1 Reply Last reply
                0
                • A Anonymous_Banned275

                  Unfortunately that is NOT the issue
                  the issues is how to do this - in pseudo code

                  hci[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

                  Pl45m4P Offline
                  Pl45m4P Offline
                  Pl45m4
                  wrote on last edited by Pl45m4
                  #8

                  @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 and hci1
                  Isnt there a better way for this?


                  If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                  ~E. W. Dijkstra

                  A 1 Reply Last reply
                  0
                  • Pl45m4P Pl45m4

                    @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 and hci1
                    Isnt there a better way for this?

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

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

                    1 Reply Last reply
                    0
                    • A Anonymous_Banned275

                      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 )

                      JonBJ Online
                      JonBJ Online
                      JonB
                      wrote on last edited by
                      #10

                      @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 of QRegularExpression::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?

                      1 Reply Last reply
                      1
                      • JonBJ JonB referenced this topic on

                      • Login

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