Solved Find exact match for substring in a QString using QRegularExpression
-
I woud like to find exact match of a substring from QString. For instance, i have a file like
something_plugin32.dll
and
something_plugin32d.dll
in a directory and i would like to load appropriate plugin based on current Debug/Release configuration.I did something like this:
#ifdef NDEBUG QRegularExpression regEx("_plugin32"); #endif #ifdef _DEBUG QRegularExpression regEx("_plugin32d"); #endif
and i try to find all plugins with suffix
_plugin32
or_plugin32d
while (directoryIterator.hasNext()) { QString fileName = directoryIterator.fileName(); QRegularExpressionMatch expMatch = regEx.match(fileName, 0, QRegularExpression::PartialPreferCompleteMatch); if (expMatch.hasMatch()) { // In release configuration // lists out all the plugins containing "_plugin32" including debug versions qInfo() << "Plugin" << fileName << "found"; } }
I am not so familiar with regular expressions in general, may be my patterns are set wrong.
-
@MarKS said in Find exact match for substring in a QString using QRegularExpression:
tried QString::contains() but have the same issue. Any simpler way to achieve this?
Sometimes the simplest solution are the best:
#ifdef DEBUG QString pattern("_plugin32d"); #else QString pattern("_plugin32"); #endif QDir path("the directory"); path.setFilter(QDir::Files); for(const auto& file : path.entryInfoList(QStringList() << "*.dll")) { if(file.baseName().endsWidth(pattern)) qInfo() << "Plugin" << file.fileName() << "found"; }
-
Why do you need a regexp for this at all?
-
@Christian-Ehrlicher tried
QString::contains()
but have the same issue. Any simpler way to achieve this? -
@MarKS said in Find exact match for substring in a QString using QRegularExpression:
tried QString::contains() but have the same issue. Any simpler way to achieve this?
Sometimes the simplest solution are the best:
#ifdef DEBUG QString pattern("_plugin32d"); #else QString pattern("_plugin32"); #endif QDir path("the directory"); path.setFilter(QDir::Files); for(const auto& file : path.entryInfoList(QStringList() << "*.dll")) { if(file.baseName().endsWidth(pattern)) qInfo() << "Plugin" << file.fileName() << "found"; }
-
@KroMignon This is a nice way to address this issue. But i had additional criterions which i forgot to mention in the question like:
it must work for.so
extensions as well and my directory has multiple subdirectories. But your answer provided me with sufficient idea to implement it myself. I implemented it something like this:-
To compare with substring i used
endsWith(pattern)
-
I used
QDirIterator
for iterating through files and subdirectories
#ifdef NDEBUG QString pattern("_plugin32"); #else QString pattern("_plugin32d"); #endif while (directoryIterator.hasNext()) { // get the file name and remove extensions like .so or .dll QString fileName = directoryIterator.fileName().split('.')[0]; if (fileName.endsWith(pattern)) { qInfo() << "Plugin" << fileName << "found"; } }
Thanks for the help!
-
-
@MarKS said in Find exact match for substring in a QString using QRegularExpression:
// get the file name and remove extensions like .so or .dll
QString fileName = directoryIterator.fileName().split('.')[0];This could be simplified with
QString fileName = directoryIterator.fileInfo().baseName();
-
@KroMignon Indeed!