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.4k 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.
  • m.sueM m.sue

    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

    raven-worxR Offline
    raven-worxR Offline
    raven-worx
    Moderators
    wrote on 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
    • raven-worxR raven-worx

      @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)

      VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on 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

      raven-worxR 1 Reply Last reply
      2
      • VRoninV VRonin

        @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]

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on 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 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;
          

          }

          raven-worxR 1 Reply Last reply
          2
          • SeenuS Seenu

            @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;
            

            }

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on 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
            • raven-worxR raven-worx

              @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 last edited by
              #9

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

              jsulmJ 1 Reply Last reply
              0
              • SeenuS Offline
                SeenuS Offline
                Seenu
                wrote on last edited by
                #10

                @Shahmisufi

                Try the code once which i posted ....

                1 Reply Last reply
                2
                • S Shahmisufi

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

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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 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
                    2
                    • SeenuS Seenu

                      @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 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

                      • Login

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