PDA

View Full Version : QRegExp wild card matching problems



JPNaude
2nd December 2011, 13:41
Hi Guys

I'm struggling to figure out how to match sets of characters using QRegExp in Wildcard pattern syntax.

I've tried many things but nothing wants to work. I'm trying to match a set of files ending in .flw and .ngc and I've tried the following:



QRegExp rx("*.flw *.ngc");
QRegExp rx("[*.flw *.ngc]");
QRegExp rx("[*.flw][*.ngc");
QRegExp rx("[*.flw]+[*.ngc]");


I've read the documentation but does not understand what means exactly:


[...] Sets of characters can be represented in square brackets, similar to full regexps. Within the character class, like outside, backslash has no special meaning.

Any guidance will be appreciated.
Thanks,
Jaco

Oleg
2nd December 2011, 14:38
Try this

QRegExp rx("*.flw|*.ngc");

NullPointer
2nd December 2011, 14:40
I've not tested this in QT, but this is valid for POSIX Regexp:



QRegExp rx(".*\.(flw|ngc)$");


HTH.

JPNaude
5th December 2011, 06:01
Hi Guys

Thanks for the answers. Unfortunately it still does now work for me. I'm going to post a bit more of my code, maybe the problem is not in the expression itself...



QDir dir("Some Dir Path");
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);
QStringList entry_list = dir.entryList();

QString filter = "*.*";
QRegExp reg_exp(filter,Qt::CaseInsensitive,QRegExp::Wildca rd);
QStringList filtered_list = entry_list.filter(reg_exp);


The above filter works fine, it gives me all the files in entry_list. However none of the other ones work. So far I've tried:



filter = "*.flw|*.ngc";
filter = ".*\.(flw|ngc)$";
filter = "*.flw *.ngc";
filter = "[*.flw *.ngc]";
filter = "[*.flw][*.ngc]";
filter = "[*.flw]+[*.ngc]";
filter = "[*.flw]|[*.ngc]";


Any suggestions will still be appreciated,
Thanks,
Jaco

Oleg
5th December 2011, 11:00
I don't know how to use QRegExp with multiple wildcard masks, but the following code will do the same task:


QDir dir("Some Dir Path");
dir.setFilter(QDir::Files | QDir::Hidden | QDir::NoSymLinks);

QStringList filters;
filters << "*.flw" << "*.ngc";
dir.setNameFilters(filters);

QStringList filtered_list = dir.entryList();

JPNaude
5th December 2011, 11:53
Thanks Oleg, that works.