PDA

View Full Version : A problem with RegExp



8Observer8
15th August 2013, 21:06
Hi,

I have the string: "Valencia 7x17"

I want to see:


0
"Valencia 7x17"
"Valencia"
"7"
"17"

But I see:


0
"Valencia 7x17"
"Valencia"
"17"
""

main.cpp


#include <QCoreApplication>
#include <QRegExp>
#include <QtDebug>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

// ([\w]+) ([\d])x([\d]+)\/([\d]x[\d]+) ([A-Z]([\d.]+)) .+ ([\w]+)
// Valencia 7x17/5x112 D70.1 ET48 Silver

QRegExp reCap("^([\\w]+)\\s([\\d])x([\\d]+)$");
qDebug() << reCap.indexIn("Valencia 7x17");
qDebug() << reCap.cap(0);
qDebug() << reCap.cap(1);
qDebug() << reCap.cap(3);
qDebug() << reCap.cap(4);

return a.exec();
}


Thank you!

ChrisW67
15th August 2013, 23:00
Why would you expect anything else? There are only three capture groups in the regular expression and you are asking for groups 1,3, and 4!


QRegExp reCap("^([\\w]+)\\s([\\d])x([\\d]+)$");
qDebug() << reCap.indexIn("Valencia 7x17");
qDebug() << reCap.cap(0);
qDebug() << reCap.cap(1);
qDebug() << reCap.cap(2);
qDebug() << reCap.cap(3);

8Observer8
16th August 2013, 04:55
Thank you very much :D