Solved QRegExp floating point matching
-
i want to use foating points in a line and i tried to do that
if(line.contains("lineto")) { QRegExp rxValues("\\d+\\.\\d+"); // primitive floating point matching rxValues.indexIn(line); out << "obj.lineTo(" + rxValues.cap(1) + ", " + rxValues.cap(2) << "\n"; qDebug() << rxValues.cap(1); qDebug() << line; }
debug is like that
""
"512.00 512.00 lineto"
""
"512.00 0.00 lineto"
""
"0.00 0.00 lineto"
""
"0.00 512.00 lineto"why it doest work? why rxValues.cap(1) is empty?
-
Hi,
Because you're not capturing anything with that regexp. It should be
"(\\d+)\\.(\\d+)"
On a side note, if you are using Qt 5, consider moving to QRegularExpression.
-
thanks for your fast reply.
i tried your suggestion and it works like that
qDebug() << rxValues.cap(1);
qDebug() << rxValues.cap(2);
qDebug() << line;"512"
"00"
"512.00 511.00 lineto"but i want the number "512.00" and "511.00"
as u see cap(1) = 512 and cap(2) = 00
and i was waiting cap(3) = 511 but i was "" nothing.
-
QRegularExpression reg("(\\d+\\.\\d+)"); qDebug() << reg.match("512.00 512.00 lineto");
-
Sorry, I misunderstood your original problem, @jsulm suggestion is the best way to go.
-
Thanks for your answers.
i used QRegularExpression and write my code like below. i think captured(1) is second number for me but debug is like that captured(0) and captured(1) is same but i didnt why.
i think;
captured(0) has to be 512.00 and captured(1) has to be 0.00QString line = "512.00 0.00 lineto"; QRegularExpression rxValues("(\\d+\\.\\d+)"); QRegularExpressionMatch match = rxValues.match(line); qDebug()<< match.captured(0); qDebug()<< match.captured(1); qDebug()<< line;
debug output
"512.00"
"512.00"
"512.00 0.00 lineto" -
QRegularExpressionMatchIterator iter = reg.globalMatch("512.00 2.00 lineto"); while (iter.hasNext()) { qDebug() << iter.next().captured(); }