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. Extract a QString with a regular expression.

Extract a QString with a regular expression.

Scheduled Pinned Locked Moved Solved General and Desktop
qregexpqstringextractsplit
12 Posts 4 Posters 11.2k 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.
  • P Patou355

    Hi all!

    I'm trying to do the followin :
    I have a QString str containing exactly what follows :
    [1] "Johnny Clegg" "B" "C" "D" "E" "F" "G" "H"
    And I want to get a QStringList containing :

    Johnny Clegg
    B
    C
    D
    E
    F
    G
    H

    the split function doesn't match what I need because I want to extract a pattern and not split from a pattern. The problem with splitting is that the first part containing [1] " will be with Johnny Clegg in the first item of the QStringList

    So the question: how to make a QStringList from a pattern extraction in a simple way? maybe as simple as QString::split ?

    mrjjM Offline
    mrjjM Offline
    mrjj
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @Patou355
    There is no way you can get a comma between the fields?

    1 Reply Last reply
    0
    • P Offline
      P Offline
      Patou355
      wrote on last edited by
      #3

      I could search and replace looking for " " but it won't solve this problem of the [1] at the beginning. I't a solution I explored before... no success :/

      mrjjM J.HilkJ 2 Replies Last reply
      0
      • P Patou355

        I could search and replace looking for " " but it won't solve this problem of the [1] at the beginning. I't a solution I explored before... no success :/

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #4

        @Patou355
        well im not sure how complex the input will be.
        For a fast solution, i would just split at space and check the entries.
        If not ending in ' it was falsely split and should be merged with next.
        if format always is like shown. its very easy to fix up.

        1 Reply Last reply
        0
        • P Patou355

          I could search and replace looking for " " but it won't solve this problem of the [1] at the beginning. I't a solution I explored before... no success :/

          J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on last edited by
          #5

          @Patou355
          Do exactly that and from the resulting QStringlist, simply remove the first entry.

          myList.removeFirst();
          

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

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

            Hi,

            Do you mean the pattern is one uppercased letter between two single quotes ?

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

            P 1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              Do you mean the pattern is one uppercased letter between two single quotes ?

              P Offline
              P Offline
              Patou355
              wrote on last edited by
              #7

              @SGaist the pattern can be anything between the double quotes. That's why I replaced the "A" with "Johnny Clegg" ;)
              I could split on the spaces but the issue here is that I would get
              Johnny
              Clegg
              B
              C
              and so on

              instead of the
              Johnny Clegg
              B
              C
              and so on

              that I want.

              J.HilkJ 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                @Patou355
                Do exactly that and from the resulting QStringlist, simply remove the first entry.

                myList.removeFirst();
                
                P Offline
                P Offline
                Patou355
                wrote on last edited by
                #8

                @J.Hilk
                The problem is that I don't always have a [1] at the beginning. Sometimes I have nothing. The cleanest solution in my case is actually extracting instead of splitting...

                1 Reply Last reply
                0
                • P Patou355

                  @SGaist the pattern can be anything between the double quotes. That's why I replaced the "A" with "Johnny Clegg" ;)
                  I could split on the spaces but the issue here is that I would get
                  Johnny
                  Clegg
                  B
                  C
                  and so on

                  instead of the
                  Johnny Clegg
                  B
                  C
                  and so on

                  that I want.

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #9

                  @Patou355
                  ok, how about this:

                  Search the string for [ and ] and remove those and everything in between

                  int start = myString.indexOf('[');
                  int end = myString.indexOf(']');
                  
                  if(start != -1 && end != -1){
                  myString.replace(start, end-start, '');
                  }
                  

                  Now split over ' '.


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

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

                    Something like ?

                    QRegularExpression rx("\"(\\w*\\s?\\w*)\"");
                    QRegularExpressionMatchIterator rxIterator = rx.globalMatch(yourCoolString);
                    QStringList words;
                    while (rxIterator.hasNext()) {
                        QRegularExpressionMatch match = rxIterator.next();
                        QString word = match.captured(1);
                        words << word;
                    }
                    

                    [edit: Fixed double quote escaping SGaist]

                    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
                    3
                    • P Offline
                      P Offline
                      Patou355
                      wrote on last edited by
                      #11

                      I wrote this function which looks a lot like @SGaist's one and that works:

                          /*Create a QStringList from the extraction of a QRegularExpression*/
                          static QStringList extractStr(const QString &s, QRegularExpression delim){
                              QStringList output;
                              QString buff;
                              QRegularExpressionMatchIterator i = delim.globalMatch(s);
                              while (i.hasNext()) {
                                  QRegularExpressionMatch match = i.next();
                                  if (match.hasMatch()) {
                                      buff = match.captured(0);
                                      output.push_back(buff);
                                  }
                              }
                              return output;
                          }
                      

                      Thanks to all of you for your help :)
                      Patrick.

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

                        One small note, you are wasting CPU cycle here. The QRegularExpressionMatchIterator returns only match objects that contains a match thus the if is useless.

                        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
                        1

                        • Login

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