Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Find exact match for substring in a QString using QRegularExpression

Find exact match for substring in a QString using QRegularExpression

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.7k Views
  • 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.
  • MarKSM Offline
    MarKSM Offline
    MarKS
    wrote on last edited by MarKS
    #1

    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.

    1 Reply Last reply
    0
    • MarKSM MarKS

      @Christian-Ehrlicher tried QString::contains() but have the same issue. Any simpler way to achieve this?

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by KroMignon
      #4

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

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      MarKSM 1 Reply Last reply
      2
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Why do you need a regexp for this at all?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        MarKSM 1 Reply Last reply
        1
        • Christian EhrlicherC Christian Ehrlicher

          Why do you need a regexp for this at all?

          MarKSM Offline
          MarKSM Offline
          MarKS
          wrote on last edited by
          #3

          @Christian-Ehrlicher tried QString::contains() but have the same issue. Any simpler way to achieve this?

          KroMignonK 1 Reply Last reply
          0
          • MarKSM MarKS

            @Christian-Ehrlicher tried QString::contains() but have the same issue. Any simpler way to achieve this?

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by KroMignon
            #4

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

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            MarKSM 1 Reply Last reply
            2
            • KroMignonK KroMignon

              @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";
              }
              
              MarKSM Offline
              MarKSM Offline
              MarKS
              wrote on last edited by
              #5

              @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!

              KroMignonK 1 Reply Last reply
              0
              • MarKSM MarKS

                @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!

                KroMignonK Offline
                KroMignonK Offline
                KroMignon
                wrote on last edited by KroMignon
                #6

                @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();

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                MarKSM 1 Reply Last reply
                4
                • KroMignonK KroMignon

                  @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();

                  MarKSM Offline
                  MarKSM Offline
                  MarKS
                  wrote on last edited by
                  #7

                  @KroMignon Indeed!

                  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 Extensions
                  • Unsolved