QRegExp帮助,RegEx一般

时间:2013-01-17 14:22:16

标签: c++ linux qt

我正在尝试学习RegEx。我的任务是从几百个* .png文件中生成一个QPixmap。理想情况下,它将是PixMap矩阵。

我认为QRegEx是执行此操作的最佳方式,因此我可以将pixmaps插入矩阵而无需排序。

我的模式我想要匹配:

runner_(int)_(int).png

第一个整数有[-1,13]和第二个[00,20]。第二个整数上有一个前导零。

这是我的代码尝试:

    // 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";
        }
    }

我的问题:请帮助RegEx表达式。

请保持温和,我是RegEx的新手(大约一小时前开始学习它!)

谢谢:)

1 个答案:

答案 0 :(得分:1)

{1,1}绝对无用,意味着使用1到1次之间的东西,即一次。你可以在字符串中写下元素。

由于你已经完成了所有正确的模式,你可以直接从它构建正则表达式:

runner_(-1|[0-9]|0+[0-9]|0*1[0123])_([0-9]|0+[0-9]|0*1[0-9]|20)\.png

基本上只是为你范围内的所有数字编写模式。

编辑逃脱点。 再次编辑以允许前导零。