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. [solved]extract values between double quotes in qstring
QtWS25 Last Chance

[solved]extract values between double quotes in qstring

Scheduled Pinned Locked Moved General and Desktop
18 Posts 6 Posters 24.4k 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.
  • U Offline
    U Offline
    utcenter
    wrote on 21 Feb 2013, 08:32 last edited by
    #4

    First, there are different double quotes, second, even if you split the string in a few stages with a few splitters, once the splitters are gone there is no way to tell whether a string fragment was in quotes.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 21 Feb 2013, 08:39 last edited by
      #5

      You might be interested by using RegExps

      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
      • Q Offline
        Q Offline
        qxoz
        wrote on 21 Feb 2013, 08:45 last edited by
        #6

        As SGaist says "RegExp":http://qt-project.org/doc/qt-4.8/qregexp.html would be a good option.

        1 Reply Last reply
        0
        • U Offline
          U Offline
          utcenter
          wrote on 21 Feb 2013, 08:45 last edited by
          #7

          Learning regular expressions will be considerably more challenging and time consuming than writing a simple parsing loop like this one:

          @QString str("Abcd(efhj:xyz; abc:” hello”; pqr:“hi”)");
          QStringList list;
          bool isValid = 0;
          int s = 0, f = 0;

          for (int i = 0; i < str.length(); ++i) {
          if (str[i] == 8220 || str[i] == 8221 || str[i] == 34) {
          if (isValid) {
          f = i;
          list.append(str.mid(s + 1, f - s - 1));
          isValid = 0;
          }
          else {
          s = i;
          isValid = 1;
          }
          }
          }

          for (int i = 0; i < list.length(); ++i ) qDebug() << list.at(i);@

          If could be done more sophisticated by tracking for opening and closing quotes as well as depth in case of nesting.

          That doesn't mean learning regular expressions will not be beneficial, especially in the long run.

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qxoz
            wrote on 21 Feb 2013, 08:52 last edited by
            #8

            Ok.If RegExp hard for first time try this:
            @
            QString MainWindow::getDataBetween(QString begin,QString end, QString &source)
            {
            int startIndex = source.indexOf(begin)+begin.length();
            if(startIndex <= 0)return QString();
            int endIndex = source.indexOf(end,startIndex);
            if(endIndex <= 0)return QString();
            return source.mid(startIndex,endIndex - startIndex);
            }@

            then call it like:

            @QString str1 = getDataBetween(":"",""",sourceString);@

            1 Reply Last reply
            0
            • U Offline
              U Offline
              utcenter
              wrote on 21 Feb 2013, 09:20 last edited by
              #9

              Won't work for his particular task. He uses extended characters, plus this method returns a single string, not all in quotes.

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jazzycamel
                wrote on 21 Feb 2013, 09:31 last edited by
                #10

                Whilst reg exp's are a complex subject, they really are worth learning and should be a weapon in every developers arsenal. Therefore, I feel honor bound to provide the reg exp solution for this as follows:

                @
                #include <QtCore>
                #include <QDebug>

                int main(int argc, char *argv[])
                {
                QCoreApplication a(argc, argv);

                QRegExp re("\"([A-Za-z0-9_\\./\\-\\s]*)\"");
                QString str="Abcd(efhj:xyz; abc:\\" hello\"; pqr:\"hi\")";
                QStringList list;
                int pos=0;
                
                while((pos=re.indexIn(str, pos))!=-1){
                    list << re.cap(1);
                    pos+=re.matchedLength();
                }
                
                qDebug() << "LIST:" << list;
                

                }
                @

                Which results in:

                LIST: (" hello", "hi")

                Hope this helps ;o)

                For the avoidance of doubt:

                1. All my code samples (C++ or Python) are tested before posting
                2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
                1 Reply Last reply
                0
                • Q Offline
                  Q Offline
                  qxoz
                  wrote on 21 Feb 2013, 09:38 last edited by
                  #11

                  My English not good, can you please tell me what is "extended characters"?
                  For getting all strings:
                  @QString str1 = getDataBetween("abc:"",""",sourceString);
                  QString str2 = getDataBetween("pqr:"",""",sourceString);@

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    devfeel
                    wrote on 21 Feb 2013, 10:02 last edited by
                    #12

                    thanks all
                    my qstring itself as double quotes means
                    wen i use qDebug it shows
                    "Abcd(efhj:xyz; abc:” hello”; pqr:“hi”)"
                    so what ever i do i am ending up with whole string again.
                    How to get rid of double quotes from whole string so that i can easily fetch hi and hello

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      jazzycamel
                      wrote on 21 Feb 2013, 10:14 last edited by
                      #13

                      "QString":http://qt-project.org/doc/qt-4.8/qstring.html provides a lot of functionality for string manipulation, the following is probably the simplest:

                      @
                      ...
                      if(myString.startsWith(""") myString.remove(0,1);
                      if(myString.endsWith(""") myString.remove(myString.size()-1,1);
                      ...
                      @

                      For the avoidance of doubt:

                      1. All my code samples (C++ or Python) are tested before posting
                      2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
                      1 Reply Last reply
                      0
                      • U Offline
                        U Offline
                        utcenter
                        wrote on 21 Feb 2013, 10:19 last edited by
                        #14

                        qDebug() shows "" for strings, but that doesn't mean the quotes are in the string, it is just decoration to signify it is a string.

                        The example I posted above will do exactly what you want with the input you specified.

                        @qxoz - " is different from ” is different from ” - " is ASCII 34, his string has extended ASCII characters that are equivalent to 8220 and 8221

                        Also, your solution won't apply to an arbitrary string, only to the one given as an example and relies on knowing what the string is exactly in advance. The solution needs to be more generic and able to work both on the example string as well as on some other.

                        1 Reply Last reply
                        0
                        • U Offline
                          U Offline
                          utcenter
                          wrote on 21 Feb 2013, 10:32 last edited by
                          #15

                          [quote author="jazzycamel" date="1361439115"]Whilst reg exp's are a complex subject, they really are worth learning and should be a weapon in every developers arsenal. Therefore, I feel honor bound to provide the reg exp solution for this as follows[/quote]

                          No argument here - the regex solution is the fastest to implement, but it is also complete magic to a newbie. It involves what appears to be a magical string and mysterious methods. While it gets the job done, it is not very educational.

                          1 Reply Last reply
                          0
                          • J Offline
                            J Offline
                            jazzycamel
                            wrote on 21 Feb 2013, 10:43 last edited by
                            #16

                            [quote author="utcenter" date="1361442748"]No argument here - the regex solution is the fastest to implement, but it is also complete magic to a newbie. It involves what appears to be a magical string and mysterious methods. While it gets the job done, it is not very educational.[/quote]

                            The problem is regexp's (like anything) will remain "magic" until you take the time to understand them.

                            And I'm not sure about not being very educational: IMHO, the problem here is simple and well bounded with defined input and output and a clear, open solution. Along with the abundance of documentation available for "QRegExp":http://qt-project.org/doc/qt-4.8/qregexp.html and regexp's in general, this is an ideal opportunity to learn about a very powerful technique early on and not get into the habit of coding around it.

                            For the avoidance of doubt:

                            1. All my code samples (C++ or Python) are tested before posting
                            2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
                            1 Reply Last reply
                            0
                            • U Offline
                              U Offline
                              utcenter
                              wrote on 21 Feb 2013, 10:49 last edited by
                              #17

                              As I said earlier - it is indeed beneficial to learn regular expressions. But so is building up a concept of how to parse text manually, which is a subject much better suited for a beginner. And while both manual parsing and regex can solve this problem, the two approaches are significantly different in their general application. Sorry if I sounded like I was downplaying your solution - it wasn't my intent ;)

                              1 Reply Last reply
                              0
                              • D Offline
                                D Offline
                                devfeel
                                wrote on 21 Feb 2013, 11:04 last edited by
                                #18

                                Worked!!!!!Thanks
                                [quote author="jazzycamel" date="1361439115"]Whilst reg exp's are a complex subject, they really are worth learning and should be a weapon in every developers arsenal. Therefore, I feel honor bound to provide the reg exp solution for this as follows:

                                @
                                #include <QtCore>
                                #include <QDebug>

                                int main(int argc, char *argv[])
                                {
                                QCoreApplication a(argc, argv);

                                QRegExp re("\"([A-Za-z0-9_\\./\\-\\s]*)\"");
                                QString str="Abcd(efhj:xyz; abc:\\\\" hello\"; pqr:\"hi\")";
                                QStringList list;
                                int pos=0;
                                
                                while((pos=re.indexIn(str, pos))!=-1){
                                    list << re.cap(1);
                                    pos+=re.matchedLength();
                                }
                                
                                qDebug() << "LIST:" << list;
                                

                                }
                                @

                                Which results in:

                                LIST: (" hello", "hi")

                                Hope this helps ;o)[/quote]

                                1 Reply Last reply
                                0

                                13/18

                                21 Feb 2013, 10:14

                                • Login

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