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 Number from an Alphanumeric QString
Forum Updated to NodeBB v4.3 + New Features

Extract Number from an Alphanumeric QString

Scheduled Pinned Locked Moved Solved General and Desktop
13 Posts 6 Posters 7.3k 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.
  • S Offline
    S Offline
    Shahmisufi
    wrote on 26 Jul 2017, 04:34 last edited by
    #1

    I have a QString of "s150 d300". How can I get the numbers from the QString and convert it into integer. Simply using 'toInt' is not working.

    Let say, from the QString of "s150 d300", only the number after the alphabet 'd' is meaningful to me. How can I extract the value of '300' from the string?

    Thank you very much for your time.

    R M 2 Replies Last reply 26 Jul 2017, 07:06
    0
    • S Shahmisufi
      26 Jul 2017, 04:34

      I have a QString of "s150 d300". How can I get the numbers from the QString and convert it into integer. Simply using 'toInt' is not working.

      Let say, from the QString of "s150 d300", only the number after the alphabet 'd' is meaningful to me. How can I extract the value of '300' from the string?

      Thank you very much for your time.

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 26 Jul 2017, 07:06 last edited by raven-worx
      #2

      @Shahmisufi

      QRegularExpression rx("[0-9]+");
      QRegularExpressionMatch match = rx.match("s150 d300");
      if ( match.hasMatch()  ) 
      {
          QString matched1 = match.captured(0); // matched == "150"
          QString matched2 = match.captured(1); // matched == "300"
      }
      

      if you really just want the number after the "d" only use this:

      QRegularExpression rx("d([0-9]+)");
      QRegularExpressionMatch match = rx.match("s150 d300");
      if ( match.hasMatch()  ) 
      {
          QString matched1 = match.captured(0); // matched == "300"
      }
      

      (untested)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      V S 2 Replies Last reply 26 Jul 2017, 07:14
      3
      • S Shahmisufi
        26 Jul 2017, 04:34

        I have a QString of "s150 d300". How can I get the numbers from the QString and convert it into integer. Simply using 'toInt' is not working.

        Let say, from the QString of "s150 d300", only the number after the alphabet 'd' is meaningful to me. How can I extract the value of '300' from the string?

        Thank you very much for your time.

        M Offline
        M Offline
        m.sue
        wrote on 26 Jul 2017, 07:06 last edited by m.sue
        #3

        Hi @Shahmisufi

        QString szStr="s150 d300";
        QRegExp regExp(qa(".*d(\\d*)"),Qt::CaseSensitive,QRegExp::RegExp);	//non-greedy wildcards
        if (regExp.indexIn(szStr) > 0) {
        	QString szNumber=regExp.cap(1);
        	//do something
        }
        

        -Michael

        R 1 Reply Last reply 26 Jul 2017, 07:10
        0
        • M m.sue
          26 Jul 2017, 07:06

          Hi @Shahmisufi

          QString szStr="s150 d300";
          QRegExp regExp(qa(".*d(\\d*)"),Qt::CaseSensitive,QRegExp::RegExp);	//non-greedy wildcards
          if (regExp.indexIn(szStr) > 0) {
          	QString szNumber=regExp.cap(1);
          	//do something
          }
          

          -Michael

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 26 Jul 2017, 07:10 last edited by
          #4

          @m.sue
          just to note:
          QRegExp is deprecated since Qt5. QRegularExpression should be used instead.

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          2
          • R raven-worx
            26 Jul 2017, 07:06

            @Shahmisufi

            QRegularExpression rx("[0-9]+");
            QRegularExpressionMatch match = rx.match("s150 d300");
            if ( match.hasMatch()  ) 
            {
                QString matched1 = match.captured(0); // matched == "150"
                QString matched2 = match.captured(1); // matched == "300"
            }
            

            if you really just want the number after the "d" only use this:

            QRegularExpression rx("d([0-9]+)");
            QRegularExpressionMatch match = rx.match("s150 d300");
            if ( match.hasMatch()  ) 
            {
                QString matched1 = match.captured(0); // matched == "300"
            }
            

            (untested)

            V Offline
            V Offline
            VRonin
            wrote on 26 Jul 2017, 07:14 last edited by
            #5

            @raven-worx

            \\\\d

            why the 4(i.e. 2) slashes? shouldn't it just be "d(\\d+)" ?

            Anticipating the questions: \d is the same as [0-9]

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            R 1 Reply Last reply 26 Jul 2017, 07:19
            2
            • V VRonin
              26 Jul 2017, 07:14

              @raven-worx

              \\\\d

              why the 4(i.e. 2) slashes? shouldn't it just be "d(\\d+)" ?

              Anticipating the questions: \d is the same as [0-9]

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 26 Jul 2017, 07:19 last edited by
              #6

              @VRonin
              I wanted to make clear that \d isn't related to the "d" in the string so i used [0-9].
              You are right, in the second example i was thinking too complicated. Updated my post.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              0
              • SeenuS Offline
                SeenuS Offline
                Seenu
                wrote on 27 Jul 2017, 12:10 last edited by
                #7

                @Shahmisufi ,

                You given string example "s150 d300" and in real case string can be anything. According to me your requirement is last occurrence number you required from input string. Below added works for all scenario's.

                pass any string to below function , output will be last occurance number

                this->lastoccurancenumber("s150d300");
                void lastoccurancenumber(QString str)
                {

                int final_number;
                QString s = str;
                QString s1="";
                for(int i = s.length()-1;i>=0;i--)
                {
                    QString temp = s[i];
                    QByteArray ba = temp.toLatin1();
                    char *c_str2 = ba.data();
                    char c_temp = c_str2[0];
                    if(!isalpha(c_temp))
                    {
                        qDebug()<<"string"<<s[i]<<"nu,ber is"<<temp<<endl;
                        s1.append(temp);
                        qDebug()<<"charater is"<<s1<<endl;
                    }
                    else
                    {
                        break;
                    }
                }
                
                std::reverse(s1.begin(), s1.end());
                final_number = s1.toInt();
                qDebug()<<"charater is"<<final_number<<endl;
                

                }

                R 1 Reply Last reply 27 Jul 2017, 12:16
                2
                • SeenuS Seenu
                  27 Jul 2017, 12:10

                  @Shahmisufi ,

                  You given string example "s150 d300" and in real case string can be anything. According to me your requirement is last occurrence number you required from input string. Below added works for all scenario's.

                  pass any string to below function , output will be last occurance number

                  this->lastoccurancenumber("s150d300");
                  void lastoccurancenumber(QString str)
                  {

                  int final_number;
                  QString s = str;
                  QString s1="";
                  for(int i = s.length()-1;i>=0;i--)
                  {
                      QString temp = s[i];
                      QByteArray ba = temp.toLatin1();
                      char *c_str2 = ba.data();
                      char c_temp = c_str2[0];
                      if(!isalpha(c_temp))
                      {
                          qDebug()<<"string"<<s[i]<<"nu,ber is"<<temp<<endl;
                          s1.append(temp);
                          qDebug()<<"charater is"<<s1<<endl;
                      }
                      else
                      {
                          break;
                      }
                  }
                  
                  std::reverse(s1.begin(), s1.end());
                  final_number = s1.toInt();
                  qDebug()<<"charater is"<<final_number<<endl;
                  

                  }

                  R Offline
                  R Offline
                  raven-worx
                  Moderators
                  wrote on 27 Jul 2017, 12:16 last edited by
                  #8

                  @Seenu
                  Motto: why easy when u can do it complicated

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0
                  • R raven-worx
                    26 Jul 2017, 07:06

                    @Shahmisufi

                    QRegularExpression rx("[0-9]+");
                    QRegularExpressionMatch match = rx.match("s150 d300");
                    if ( match.hasMatch()  ) 
                    {
                        QString matched1 = match.captured(0); // matched == "150"
                        QString matched2 = match.captured(1); // matched == "300"
                    }
                    

                    if you really just want the number after the "d" only use this:

                    QRegularExpression rx("d([0-9]+)");
                    QRegularExpressionMatch match = rx.match("s150 d300");
                    if ( match.hasMatch()  ) 
                    {
                        QString matched1 = match.captured(0); // matched == "300"
                    }
                    

                    (untested)

                    S Offline
                    S Offline
                    Shahmisufi
                    wrote on 27 Jul 2017, 12:52 last edited by
                    #9

                    @raven-worx I have tested the code and it returns "d300" instead of "300"

                    jsulmJ 1 Reply Last reply 28 Jul 2017, 05:30
                    0
                    • SeenuS Offline
                      SeenuS Offline
                      Seenu
                      wrote on 28 Jul 2017, 03:22 last edited by
                      #10

                      @Shahmisufi

                      Try the code once which i posted ....

                      1 Reply Last reply
                      2
                      • S Shahmisufi
                        27 Jul 2017, 12:52

                        @raven-worx I have tested the code and it returns "d300" instead of "300"

                        jsulmJ Offline
                        jsulmJ Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 28 Jul 2017, 05:30 last edited by jsulm
                        #11

                        @Shahmisufi This works for me:

                        QRegularExpression rx("[0-9]+");
                        QRegularExpressionMatchIterator matches = rx.globalMatch("s150 d300");
                        while (matches.hasNext()) {
                            QRegularExpressionMatch match = matches.next();
                            qDebug() << match.captured(0);
                        }
                        

                        @Seenu your solution might work (I didn't test it) but it is much more complex compared to a solution using a simple regular expression.

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

                        1 Reply Last reply
                        2
                        • SeenuS Offline
                          SeenuS Offline
                          Seenu
                          wrote on 28 Jul 2017, 05:56 last edited by
                          #12

                          @jsulm

                          Ya its complex , but it will return last occurrence number in a string and is in generic format

                          jsulmJ 1 Reply Last reply 28 Jul 2017, 05:57
                          2
                          • SeenuS Seenu
                            28 Jul 2017, 05:56

                            @jsulm

                            Ya its complex , but it will return last occurrence number in a string and is in generic format

                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on 28 Jul 2017, 05:57 last edited by
                            #13

                            @Seenu What do you mean by "generic format"?
                            You can do the same with a simple regular expression.

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

                            1 Reply Last reply
                            3

                            1/13

                            26 Jul 2017, 04:34

                            • Login

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