Extract Number from an Alphanumeric QString



  • 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.


  • Moderators

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



  • 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


  • Moderators

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



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


  • Moderators

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



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

    }


  • Moderators

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



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



  • @Shahmisufi

    Try the code once which i posted ....


  • Moderators

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



  • @jsulm

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


  • Moderators

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.