QString matching.



  • I have some filenames or file paths in QStringList. I need to filter string values by given mask like : .txt or moc_ or any other, like in search. If i use contains I'll get result anyvere the given mask (without '*') was found. So here is the task.



  • Look into the QStringList::filter() functions. And probably learn about regular expressions for this purpose.



  • I have two QStringLists with include and exclude values. This mean if I make a loop for target list(with my filenames) I need first look the matching to all of the includes values list, and if includes list is empty I need to look in excludes. So I make the function:
    @bool include(const QString & name)
    {
    bool bInclude = false;

    if(!indcludes.isEmpty())
    {
    for(int i = 0; i<indcludes.count();i++)
    {
    if(name.contains(indcludes.at(i)))
    bInclude = true;
    }
    }
    else
    bInclude = true;

    if(!exludes.isEmpty())
    {
    for(int i = 0; i<exludes.count();i++)
    {
    if(name.contains(exludes.at(i)))
    bInclude = false;
    }
    }

    return bInclude;
    }@
    Here I use contains() function, but it's not correct, because I need to match with mask.



  • I gather from your description that this function takes a filename and you want to check whether to include or exclude it by going through two lists with filename masks.

    Untested, but it is the first code I would try:

    @bool include(const QString &name)
    {
    bool result = includes.isEmpty();

    foreach (const QString &pattern, includes) {
        if (QRegExp(pattern, Qt::CaseInsensitive).exactMatch(name))
            result = true;
    }
    
    foreach (const QString &pattern, excludes) {
        if (QRegExp(pattern, Qt::CaseInsensitive).exactMatch(name))
            result = false;
    }
    
    return result;
    

    }@



  • Nope this code:
    @bool include(const QString &name)
    {
    bool result = includes.isEmpty();

    foreach (const QString &pattern, includes) {
        if (QRegExp(pattern, Qt::CaseInsensitive).exactMatch(name))
            result = true;
    }
    
    foreach (const QString &pattern, excludes) {
        if (QRegExp(pattern, Qt::CaseInsensitive).exactMatch(name))
            result = false;
    }
    
    return result;
    

    }@
    does not work. If i set mask *.cpp it skips all of my files. But there are lot of filename strings like this:
    .\file.cpp



  • That is likely because @".cpp"@ is not a valid regexp. Try something like @".\.cpp"@ instead.



  • I tried .*\.cpp a also tried .\file.cpp masks(which matches 100% with containing text) it's don't work at all.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.