Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString matching.
Qt 6.12 LTS is out! See what's new in the release blog

QString matching.

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 7.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anticross
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • F Offline
      F Offline
      Franzk
      wrote on last edited by
      #2

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

      "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Anticross
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          Franzk
          wrote on last edited by
          #4

          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;
          

          }@

          "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

          http://www.catb.org/~esr/faqs/smart-questions.html

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Anticross
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • F Offline
              F Offline
              Franzk
              wrote on last edited by
              #6

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

              "Horse sense is the thing a horse has which keeps it from betting on people." -- W.C. Fields

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • A Offline
                A Offline
                Anticross
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt
                • Unsolved