Parsing a QString using RegEX



  • I need to parse a QString for checking minimum number of digists in it and then need to take last 14 digits from the QString and convert them into QDate foramt. Below is my code that works for exact number of digits:
    @QString myString = "alpha20140402110241";
    QRegExp rx("(\d{14})");
    qDebug()<<"RegEx Result:" <<rx.cap(1);
    @

    How can I modify this code so that I can use this code for more 14 digits, in QString, as well?
    My RegEx should check for minimum 14 digits in my QString and only 14 digits. Afterwards , allow to me extract last 14 digits from QString using
    @rx.cap(1) @

    or any other method.



  • hi
    try this..

    QString myString = "alpha20140402110241";

    QString res;

    for(int i = myString.length()-14 ; i < myString.length();i++)

    {

    res.append(myString.at(i));

    }

    qDebug() << res

    Output :

    20140402110241


  • Moderators

    The syntax for "at least" is {14,}
    To extract only the last 14 characters you can use "QString::right()":http://qt-project.org/doc/qt-5/qstring.html#right

    Edit: @IamSumit copying characters one by one like that is very inefficient.


Log in to reply
 

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