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.
QtWS25 Last Chance

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 Offline
    P Offline
    Patou355
    wrote on 13 Mar 2017, 10:28 last edited by
    #1

    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 ?

    M 1 Reply Last reply 13 Mar 2017, 10:33
    0
    • P Patou355
      13 Mar 2017, 10:28

      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 ?

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 13 Mar 2017, 10:33 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 13 Mar 2017, 10:37 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 :/

        M J 2 Replies Last reply 13 Mar 2017, 10:40
        0
        • P Patou355
          13 Mar 2017, 10:37

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 13 Mar 2017, 10:40 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
            13 Mar 2017, 10:37

            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 Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 13 Mar 2017, 10:41 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 13 Mar 2017, 12:04
            1
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 13 Mar 2017, 10:58 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 13 Mar 2017, 11:59
              0
              • S SGaist
                13 Mar 2017, 10:58

                Hi,

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

                P Offline
                P Offline
                Patou355
                wrote on 13 Mar 2017, 11:59 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 1 Reply Last reply 13 Mar 2017, 12:07
                0
                • J J.Hilk
                  13 Mar 2017, 10:41

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

                  myList.removeFirst();
                  
                  P Offline
                  P Offline
                  Patou355
                  wrote on 13 Mar 2017, 12:04 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
                    13 Mar 2017, 11:59

                    @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 Offline
                    J Offline
                    J.Hilk
                    Moderators
                    wrote on 13 Mar 2017, 12:07 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
                    • S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 13 Mar 2017, 21:37 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 14 Mar 2017, 13:09 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
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 14 Mar 2017, 13:18 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

                          1/12

                          13 Mar 2017, 10:28

                          • Login

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