-3

How to match every String behind ":"? For example: want to match "3.23423" in "roll:3.23423" or "true" in "smth:true".

4

1 回答 1

2

尝试这个:

QRegExp rx("[a-z]+\:.+");
QString ss = "roll:3.23423";

int poss = 0;
while ((poss = rx.indexIn(ss, poss)) != -1) {
    qDebug( )<< rx.cap(0).split(":").last();
    poss += rx.matchedLength();
}

输出:

"3.23423" 

但是一位男士告诉我这split()可能会很慢,所以你也可以使用:

QRegExp rx("[a-z]+\:.+");
QString ss = "roll:3.23423";

int poss = 0;
while ((poss = rx.indexIn(ss, poss)) != -1) {

    QString g = rx.cap(0);
    int p = rx.cap(0).indexOf(":");
    qDebug( )<< g.mid(p+1);
    poss += rx.matchedLength();
}

它应该更快。

更新(之前)。使用这个循环:

while ((poss = rx.indexIn(ss, poss)) != -1) {

    QString g = rx.cap(0);
    int p = rx.cap(0).lastIndexOf(":");
    qDebug( )<< g.mid(0,p);
    poss += rx.matchedLength();
}
于 2014-09-15T15:15:07.903 回答