-3

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

user1824542
  • 225
  • 1
  • 4
  • 15

1 Answers1

2

Try this:

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();
}

Output:

"3.23423" 

But one man told me that split() can be slow, so you can use also:

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();
}

It should be faster.

Update (before). Use this loop:

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();
}
Jablonski
  • 18,083
  • 2
  • 46
  • 47