I’m in the process of attempting to learn RegEx. I’ve been tasked with generating a QPixmap out of several hundred *.png files. Ideally, it would be a PixMap matrix.
I think that QRegEx is the best way to perform this action so I can insert the pixmaps into a matrix without having to sort.
My pattern I’m trying to match:
runner_(int)_(int).png
Where the first integer has bounds [-1, 13] and the second [00, 20]. There is a leading zero on the second integer.
This is my code attempt:
// find the png files in the thing
QDir fileDir(iconPath);
QFileInfoList fileList = fileDir.entryInfoList();
QRegExp rxlen("runner_([^\\_]{1,1}])_([^\\_]{1,1}]).png");
foreach (const QFileInfo &info, fileList) {
qDebug() << info.fileName();
int pos = rxlen.indexIn(info.fileName());
if (pos > 1) {
qDebug() << rxlen.cap(1);
qDebug() << rxlen.cap(2);
} else {
qDebug() << "Didn't find any";
}
}
My question: Please help with the RegEx expression.
Please be gentle, I’m new to RegEx (started learning it about an hour ago!)
Thanks 🙂
{1,1}is absolutely useless, means something that’s used between 1 and 1 times, ie once. You can just write the element in the string.Since you already have your pattern down all nice and proper, you can just build the regex straight from it:
Basically just writing patterns for all numbers in your range.
Edited to escape the dot.
Edited again to allow leading zeroes.