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. Infinite loop with regex (non reproducible in command line application)
Qt 6.11 is out! See what's new in the release blog

Infinite loop with regex (non reproducible in command line application)

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.2k 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.
  • pgiovanniP Offline
    pgiovanniP Offline
    pgiovanni
    wrote on last edited by pgiovanni
    #1

    Let me know if you can reproduce this on your qtcreator. I have a button connected to a QlineEdit. A new window appears and is supposed to have a chart appear. But instead it gets stuck in the regex loop. The loop works in a command line app. I think the pointer std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString()); causes an error potentially. In my command line attempt passing around pointers causes a segmentation fault.

    readInMBTI is the QlineEdit name btw;

    Why is it infinitely looping???? it works elsewhere :(

    
    QWidget *wdg = new QWidget;
    wdg->show();
    
    std::ifstream file("file.txt");
       std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString());
       std::string line;
       std::string returnLine;
    
       if(MBTIstring.length() < 17) {
    
           while(getline(file, line)) {
            if(line.find(MBTIstring, 0) != std::string::npos && line.find(MBTIstring, 0) < 2) {
              returnLine = line;
              break;
            }
          }
       }
    
      std::regex r(" *(\\w+ \\w+)");
      std::smatch m;
      auto flag = std::regex_constants::match_continuous;
      while(std::regex_search(returnLine, m, r, flag)) {
           MBTIbroken.push_back(m[1]);
           MBTIstring = m.suffix().str();
       }
    
    JonBJ 2 Replies Last reply
    0
    • pgiovanniP pgiovanni

      Let me know if you can reproduce this on your qtcreator. I have a button connected to a QlineEdit. A new window appears and is supposed to have a chart appear. But instead it gets stuck in the regex loop. The loop works in a command line app. I think the pointer std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString()); causes an error potentially. In my command line attempt passing around pointers causes a segmentation fault.

      readInMBTI is the QlineEdit name btw;

      Why is it infinitely looping???? it works elsewhere :(

      
      QWidget *wdg = new QWidget;
      wdg->show();
      
      std::ifstream file("file.txt");
         std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString());
         std::string line;
         std::string returnLine;
      
         if(MBTIstring.length() < 17) {
      
             while(getline(file, line)) {
              if(line.find(MBTIstring, 0) != std::string::npos && line.find(MBTIstring, 0) < 2) {
                returnLine = line;
                break;
              }
            }
         }
      
        std::regex r(" *(\\w+ \\w+)");
        std::smatch m;
        auto flag = std::regex_constants::match_continuous;
        while(std::regex_search(returnLine, m, r, flag)) {
             MBTIbroken.push_back(m[1]);
             MBTIstring = m.suffix().str();
         }
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #6

      @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

      Why is it infinitely looping???? it works elsewhere :(

        while(std::regex_search(returnLine, m, r, flag)) {
             MBTIbroken.push_back(m[1]);
             MBTIstring = m.suffix().str();
         }
      

      Could you explain how that loop exits, assuming it enters (i.e. there is a match)? Thanks. It's pasted from your first post. And it's what I answered "No, it does not work" to.

      You seem to have changed the last line to

      returnLine = m.suffix().str();
      

      later on. Why? Is one your non-working Qt code and the other your working command line one?

      it works without qt involved man.

      In the code you show the only bit that is "Qt" is

      QWidget *wdg = new QWidget;
      wdg->show();
      

      Other than that there isn't any Qt code.

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

        @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

        std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString());

        wtf?

        while(std::regex_search(returnLine, m, r, flag))

        where do you modify returnLine in your loop?

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

        pgiovanniP 1 Reply Last reply
        2
        • pgiovanniP pgiovanni

          Let me know if you can reproduce this on your qtcreator. I have a button connected to a QlineEdit. A new window appears and is supposed to have a chart appear. But instead it gets stuck in the regex loop. The loop works in a command line app. I think the pointer std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString()); causes an error potentially. In my command line attempt passing around pointers causes a segmentation fault.

          readInMBTI is the QlineEdit name btw;

          Why is it infinitely looping???? it works elsewhere :(

          
          QWidget *wdg = new QWidget;
          wdg->show();
          
          std::ifstream file("file.txt");
             std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString());
             std::string line;
             std::string returnLine;
          
             if(MBTIstring.length() < 17) {
          
                 while(getline(file, line)) {
                  if(line.find(MBTIstring, 0) != std::string::npos && line.find(MBTIstring, 0) < 2) {
                    returnLine = line;
                    break;
                  }
                }
             }
          
            std::regex r(" *(\\w+ \\w+)");
            std::smatch m;
            auto flag = std::regex_constants::match_continuous;
            while(std::regex_search(returnLine, m, r, flag)) {
                 MBTIbroken.push_back(m[1]);
                 MBTIstring = m.suffix().str();
             }
          
          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by
          #3

          @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

          The loop works in a command line app.

          it works elsewhere :(

          No, it does not.

          pgiovanniP 1 Reply Last reply
          0
          • JonBJ JonB

            @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

            The loop works in a command line app.

            it works elsewhere :(

            No, it does not.

            pgiovanniP Offline
            pgiovanniP Offline
            pgiovanni
            wrote on last edited by pgiovanni
            #4

            No, it does not.

            it works without qt involved man. You want proof?

            JonBJ 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

              std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString());

              wtf?

              while(std::regex_search(returnLine, m, r, flag))

              where do you modify returnLine in your loop?

              pgiovanniP Offline
              pgiovanniP Offline
              pgiovanni
              wrote on last edited by pgiovanni
              #5

              m.suffix().str(); should be changing it. It works in command line app as such.

              #include <iostream>
              #include <string>
              #include <regex>
              #include <vector>
              #include <fstream>
              
              int main() {
              std::string returnLine;
              std::vector<std::string> MBTIbroken;
              std::string MBTIstring;
              std::regex r(" *(\\w+ \\w+)"); // entire match will be 2 numbers
              std::smatch m;
              std::string line;
              std::cout << "input MBTI type" << std::endl;
              getline(std::cin, MBTIstring);
              
                std::ifstream file("jcalg hardcode.txt");
              
                  if(MBTIstring.length() < 17) {
              
                      while(getline(file, line)) {
                       if(line.find(MBTIstring, 0) != std::string::npos && line.find(MBTIstring, 0) < 2) {
                         returnLine = line;
                         break;
                       }
                     }
                  }
              
                auto flag = std::regex_constants::match_continuous;
                while(std::regex_search(returnLine, m, r, flag)) {
                  MBTIbroken.push_back(m[1]);
                  returnLine = m.suffix().str();
                }
              
              
              
                int i = 0;
                std::cout << "Vector Contents:" << std::endl;
                for(auto s : MBTIbroken) {
                  std::cout << (++i) << ") " << s << std::endl;
                }
              }
              

              @Christian-Ehrlicher it gets the string input into the QLineEdit which is what i need? wym "wtf"

              1 Reply Last reply
              0
              • pgiovanniP pgiovanni

                Let me know if you can reproduce this on your qtcreator. I have a button connected to a QlineEdit. A new window appears and is supposed to have a chart appear. But instead it gets stuck in the regex loop. The loop works in a command line app. I think the pointer std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString()); causes an error potentially. In my command line attempt passing around pointers causes a segmentation fault.

                readInMBTI is the QlineEdit name btw;

                Why is it infinitely looping???? it works elsewhere :(

                
                QWidget *wdg = new QWidget;
                wdg->show();
                
                std::ifstream file("file.txt");
                   std::string MBTIstring = *new std::string(this->readInMBTI->text().toStdString());
                   std::string line;
                   std::string returnLine;
                
                   if(MBTIstring.length() < 17) {
                
                       while(getline(file, line)) {
                        if(line.find(MBTIstring, 0) != std::string::npos && line.find(MBTIstring, 0) < 2) {
                          returnLine = line;
                          break;
                        }
                      }
                   }
                
                  std::regex r(" *(\\w+ \\w+)");
                  std::smatch m;
                  auto flag = std::regex_constants::match_continuous;
                  while(std::regex_search(returnLine, m, r, flag)) {
                       MBTIbroken.push_back(m[1]);
                       MBTIstring = m.suffix().str();
                   }
                
                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #6

                @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

                Why is it infinitely looping???? it works elsewhere :(

                  while(std::regex_search(returnLine, m, r, flag)) {
                       MBTIbroken.push_back(m[1]);
                       MBTIstring = m.suffix().str();
                   }
                

                Could you explain how that loop exits, assuming it enters (i.e. there is a match)? Thanks. It's pasted from your first post. And it's what I answered "No, it does not work" to.

                You seem to have changed the last line to

                returnLine = m.suffix().str();
                

                later on. Why? Is one your non-working Qt code and the other your working command line one?

                it works without qt involved man.

                In the code you show the only bit that is "Qt" is

                QWidget *wdg = new QWidget;
                wdg->show();
                

                Other than that there isn't any Qt code.

                pgiovanniP 1 Reply Last reply
                2
                • JonBJ JonB

                  @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

                  Why is it infinitely looping???? it works elsewhere :(

                    while(std::regex_search(returnLine, m, r, flag)) {
                         MBTIbroken.push_back(m[1]);
                         MBTIstring = m.suffix().str();
                     }
                  

                  Could you explain how that loop exits, assuming it enters (i.e. there is a match)? Thanks. It's pasted from your first post. And it's what I answered "No, it does not work" to.

                  You seem to have changed the last line to

                  returnLine = m.suffix().str();
                  

                  later on. Why? Is one your non-working Qt code and the other your working command line one?

                  it works without qt involved man.

                  In the code you show the only bit that is "Qt" is

                  QWidget *wdg = new QWidget;
                  wdg->show();
                  

                  Other than that there isn't any Qt code.

                  pgiovanniP Offline
                  pgiovanniP Offline
                  pgiovanni
                  wrote on last edited by
                  #7

                  @JonB oh shit. you right. and that worked. programs crashing now, but i'll spend some time on that first. it's not the same issue.

                  1 Reply Last reply
                  0
                  • pgiovanniP pgiovanni

                    No, it does not.

                    it works without qt involved man. You want proof?

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #8

                    @pgiovanni said in Infinite loop with regex (non reproducible in command line application):

                    No, it does not.

                    it works without qt involved man. You want proof?

                    A more productive initial reaction than this would be "I wonder if these guys actually know what they're talking about, I'll go triple-check my code in case..." :) <- Smiley

                    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