Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. Parsing string
Forum Updated to NodeBB v4.3 + New Features

Parsing string

Scheduled Pinned Locked Moved Unsolved Qt 6
5 Posts 5 Posters 729 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.
  • F Offline
    F Offline
    fari35
    wrote on 1 Mar 2021, 14:21 last edited by
    #1

    I have a string <abc>0200</abc><xyz>10101</xyz>. I want to extract the values of these tags like 0200 from abc tag and 10101 from xyz tag. How can I do that in Qt?

    J 1 Reply Last reply 1 Mar 2021, 14:22
    0
    • F fari35
      1 Mar 2021, 14:21

      I have a string <abc>0200</abc><xyz>10101</xyz>. I want to extract the values of these tags like 0200 from abc tag and 10101 from xyz tag. How can I do that in Qt?

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 1 Mar 2021, 14:22 last edited by
      #2

      @fari35 said in Parsing string:

      How can I do that in Qt?

      Using https://doc.qt.io/qt-5/qregularexpression.html

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 1 Mar 2021, 14:22 last edited by
        #3

        Already answered on so: 'Use a combination of std::string::find and std::string::substr, or their QString equivalents QString::indexOf and QString::mid.'

        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
        1
        • K Offline
          K Offline
          Ketan__Patel__0011
          wrote on 1 Apr 2021, 16:11 last edited by Ketan__Patel__0011 4 Jan 2021, 16:12
          #4

          Here I have Developed logic for you..
          you can try it.

              QString Check = "<abc>0200</abc><xyz>10101</xyz>"; /// YOUR STRING
              QVector<QRegExp> rx;
              rx.push_back(QRegExp("<abc>([a-zA-Z0-9 ]+)</abc>"));
              rx.push_back(QRegExp("<xyz>([a-zA-Z0-9 ]+)</xyz>"));
          
              QStringList list;
          
              for(int i = 0 ; i < rx.size() ; i++)
              {
                  int pos = 0;
                  while ((pos = rx.at(i).indexIn(Check, pos)) != -1)
                  {
                      list << rx.at(i).cap(1);
                      pos += rx.at(i).matchedLength();
                  }
              }
          

          Here i have used QRegExp Class.
          i have add tag names in Expression And extract the data between from it and store the all data in QStringList

          You just have to make Expression and add in QVector<QRegex> as i used.

          S 1 Reply Last reply 1 Apr 2021, 18:55
          0
          • K Ketan__Patel__0011
            1 Apr 2021, 16:11

            Here I have Developed logic for you..
            you can try it.

                QString Check = "<abc>0200</abc><xyz>10101</xyz>"; /// YOUR STRING
                QVector<QRegExp> rx;
                rx.push_back(QRegExp("<abc>([a-zA-Z0-9 ]+)</abc>"));
                rx.push_back(QRegExp("<xyz>([a-zA-Z0-9 ]+)</xyz>"));
            
                QStringList list;
            
                for(int i = 0 ; i < rx.size() ; i++)
                {
                    int pos = 0;
                    while ((pos = rx.at(i).indexIn(Check, pos)) != -1)
                    {
                        list << rx.at(i).cap(1);
                        pos += rx.at(i).matchedLength();
                    }
                }
            

            Here i have used QRegExp Class.
            i have add tag names in Expression And extract the data between from it and store the all data in QStringList

            You just have to make Expression and add in QVector<QRegex> as i used.

            S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 1 Apr 2021, 18:55 last edited by
            #5

            @Ketan__Patel__0011 please do not use QRegExp, it was deprecated in Qt 5 and removed in Qt 6. Use QRegularExpression.

            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
            2

            • Login

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