[Solved] Qt regexp

  • Hi People,

    I have a little bit problem with using of QRegExp class. Please consider the following dummy example:
    QString str = "sadsfsdfg52151";
    QRegExp r("^([A-Za-Z]+)([0-9]+)");

    qDebug() << r.cap(0);
    qDebug() << r.cap(1);
    qDebug() << r.cap(2);


    I hope that those find full string via "cap(0)", the first part of "str" via "cap(1)" and the numbers at end of the string via "cap(2)", but it doesn't print anything. What I made wrong? :(
    Can anybody help me, how to write a properly regexp's with Qt?


  • Your syntax is wrong, the QRegExp should read:

    QRegExp r("^([A-Za-z]+)([0-9]+)");

    Note that the second 'z' is not written in caps any more.

    Disclaimer: tested with Perl only.

  • Lifetime Qt Champion


    You can also use:
    @QRegExp r("^([A-Za-z]+)(\d+)");@

Log in to reply

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