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. How to split QString using QRegex?

How to split QString using QRegex?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 2.4k Views 1 Watching
  • 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.
  • N Offline
    N Offline
    npatil15
    wrote on last edited by
    #1

    Hello,

    I want to split this: {($..Properties[*])-($..Layer_Index)}
    Like: ("$..Properties[*]", "$..Layer_Index")

    I have tried some examples and online regex tester, I'm getting output but it's not working here.

    On this link: Regex Online Tester
    I have tried with \(.*?\)
    But in code, it's not working.

    I have tried with many expressions but not working

    What I'm missing?

    1 Reply Last reply
    0
    • N Offline
      N Offline
      npatil15
      wrote on last edited by
      #9

      I got my mistake,

      instead of using re.match(query), I have to used re.globalMatch(query) to get all matched information.

      Thanks for all of yours efforts.

      1 Reply Last reply
      0
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @npatil15 said in How to split QString using QRegex?:

        But in code, it's not working.

        First please use QRegularExpression, second please show us the code. Be aware that you have to escape '\'.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        4
        • N Offline
          N Offline
          npatil15
          wrote on last edited by npatil15
          #3

          I have tried the below method.

          For reference, I have used below code

              QString query("{($..Properties[*])-($..Layer_Index)}");
              QStringList list = query.split(QRegularExpression("\\(.*?\\)"));
              qDebug()<<"Output: "<<list;
          
          //Output --> Output:  ("{", "-", "}")
          

          And I have checked on many online regex testers like this one https://regexr.com/
          And output is like bold marked: {($..Properties[*])-($..Layer_Index)}

          So why there is so differences in this outputs.

          KroMignonK 1 Reply Last reply
          0
          • N npatil15

            I have tried the below method.

            For reference, I have used below code

                QString query("{($..Properties[*])-($..Layer_Index)}");
                QStringList list = query.split(QRegularExpression("\\(.*?\\)"));
                qDebug()<<"Output: "<<list;
            
            //Output --> Output:  ("{", "-", "}")
            

            And I have checked on many online regex testers like this one https://regexr.com/
            And output is like bold marked: {($..Properties[*])-($..Layer_Index)}

            So why there is so differences in this outputs.

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #4

            @npatil15 Hmm, I think you should do it like this:

            QStringList list = query.split(QRegularExpression("({\\(|\\)-\\(|\\)})"), QString::SkipEmptyParts);
            

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            N 1 Reply Last reply
            3
            • KroMignonK KroMignon

              @npatil15 Hmm, I think you should do it like this:

              QStringList list = query.split(QRegularExpression("({\\(|\\)-\\(|\\)})"), QString::SkipEmptyParts);
              
              N Offline
              N Offline
              npatil15
              wrote on last edited by
              #5

              @KroMignon said in How to split QString using QRegex?:

              @npatil15 Hmm, I think you should do it like this:

              QStringList list = query.split(QRegularExpression("({\\(|\\)-\\(|\\)})"), QString::SkipEmptyParts);
              

              Thanks a lot, it works like charm. :)

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                Hi,

                Out of curiosity, are you needing to split ? Otherwise, you could also use QRegularExpession::match and extract each side from the returned QRegularExpressionMatch object. It might look a bit more complicated but you will also be more precise in what you are extracting.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • N Offline
                  N Offline
                  npatil15
                  wrote on last edited by npatil15
                  #7

                  @SGaist ,

                  Yes I'm about to drop reply.

                  The solution mentioned by @KroMignon , it works if I have specific expressions, but I may have multiple expressions like: {($..Properties[*])-($..Layer_Index)+($..Name==Value())/($..Properties(Name==True))}

                  So here in the above expression, I want to split text like:

                  • $..Properties[*]
                  • $..Layer_Index
                  • $..Name==Value()
                  • $..Properties(Name==True)

                  And more over want to split the math symbol like, -,+,/. To do the mathematical operations.

                  I have tried with your suggestions as mentioned in below code:

                      QString query("{($..Properties[*])-($..Layer_Index)+($..Name==Value())/($..Properties(Name==True))}");
                      QRegularExpression re;
                      re.setPattern("\\(.*?\\)");
                      QRegularExpressionMatch match = re.match(query);
                      qDebug()<<"Match 0: "<<match.captured(0);
                      qDebug()<<"Match 1: "<<match.captured(1);
                      qDebug()<<"Match 2: "<<match.captured(2);
                  
                  Output:
                  Match 0:  "($..Properties[*])"
                  Match 1:  ""
                  Match 2:  ""
                  

                  I think I'm passing incorrect pattern, which I didnt understanding, how to split all between the ( ) brackets but not the brackets inside bracket like Name==True from ($..Properties(Name==True)).
                  What I'm missing here to get a list of all split strings?

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #8

                    Do you have some sort of DSL ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • N Offline
                      N Offline
                      npatil15
                      wrote on last edited by
                      #9

                      I got my mistake,

                      instead of using re.match(query), I have to used re.globalMatch(query) to get all matched information.

                      Thanks for all of yours efforts.

                      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