Qt 正则表达式取值

精确匹配:

void testQRegex(){
    QRegExp reg("aaa=(\\d+)");
    QString in = "aaa=11";
    bool result = reg.exactMatch(in);
    logDebug() << result;
    if(result){
        logDebug() << reg.cap(1);
    }

}
testQRegex ( ../qt-training/main.cpp : 43 ) true

testQRegex ( ../qt-training/main.cpp : 45 ) "11"

循环遍历:

void testQRegex2(){
    QRegExp rx("(\\d+)");
    QString str = "Offsets: 12 14 99 231 7";
    QStringList list;
    int pos = 0;

    while ((pos = rx.indexIn(str, pos)) != -1) {
        list << rx.cap(1);
        pos += rx.matchedLength();
    }
    logDebug() << list;
}
testQRegex2 ( ../qt-training/main.cpp : 60 ) ("12", "14", "99", "231", "7")

猜你喜欢

转载自blog.csdn.net/hknaruto/article/details/107002296