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




    qDebug() << res

    Output :


  • Moderators

    The syntax for "at least" is {14,}
    To extract only the last 14 characters you can use "QString::right()":

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

Log in to reply