PDA

View Full Version : need help with my regex



patcito
29th May 2006, 18:35
I have an process that outputs lines like that:
0,0,1 some blah
0,1,0 some blah
1,0,1 some blah
2,0,1 some blah

I want to grab the first numeric part, this is how I do it:


QString result = cdscanbus.readAllStandardOutput();
qDebug()<< result;
QRegExp rx("[0-9],[0-9],[0-9]");
int pos = rx.indexIn(result);
if(pos > -1){
QString value = rx.cap(0);
qDebug()<< value;
}


the problem is that qDebug only returns the first 0,0,0 expression.
any idea how to make it return all occurence of [0-9],[0-9],[0-9] ?
thanx in advance

Pat

jacek
29th May 2006, 18:39
There's an example in the docs (http://doc.trolltech.com/4.1/qregexp.html#indexIn):

QString str = "offsets: 1.23 .50 71.00 6.00";
QRegExp rx("\\d*\\.\\d+"); // primitive floating point matching
int count = 0;
int pos = 0;
while ((pos = rx.indexIn(str, pos)) != -1) {
++count;
pos += rx.matchedLength();
}
// pos will be 9, 14, 18 and finally 24; count will end up as 4