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)
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 622 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.
  • P Offline
    P Offline
    pgiovanni
    wrote on 28 Nov 2021, 15:13 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();
       }
    
    J 2 Replies Last reply 28 Nov 2021, 15:28
    0
    • P pgiovanni
      28 Nov 2021, 15:13

      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();
         }
      
      J Offline
      J Offline
      JonB
      wrote on 28 Nov 2021, 16:25 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.

      P 1 Reply Last reply 28 Nov 2021, 17:13
      2
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 28 Nov 2021, 15:24 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

        P 1 Reply Last reply 28 Nov 2021, 15:31
        2
        • P pgiovanni
          28 Nov 2021, 15:13

          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();
             }
          
          J Offline
          J Offline
          JonB
          wrote on 28 Nov 2021, 15:28 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.

          P 1 Reply Last reply 28 Nov 2021, 15:29
          0
          • J JonB
            28 Nov 2021, 15:28

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

            P Offline
            P Offline
            pgiovanni
            wrote on 28 Nov 2021, 15:29 last edited by pgiovanni
            #4

            No, it does not.

            it works without qt involved man. You want proof?

            J 1 Reply Last reply 28 Nov 2021, 17:49
            0
            • C Christian Ehrlicher
              28 Nov 2021, 15:24

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

              P Offline
              P Offline
              pgiovanni
              wrote on 28 Nov 2021, 15:31 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
              • P pgiovanni
                28 Nov 2021, 15:13

                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();
                   }
                
                J Offline
                J Offline
                JonB
                wrote on 28 Nov 2021, 16:25 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.

                P 1 Reply Last reply 28 Nov 2021, 17:13
                2
                • J JonB
                  28 Nov 2021, 16:25

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

                  P Offline
                  P Offline
                  pgiovanni
                  wrote on 28 Nov 2021, 17:13 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
                  • P pgiovanni
                    28 Nov 2021, 15:29

                    No, it does not.

                    it works without qt involved man. You want proof?

                    J Offline
                    J Offline
                    JonB
                    wrote on 28 Nov 2021, 17:49 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

                    1/8

                    28 Nov 2021, 15:13

                    • Login

                    • Login or register to search.
                    1 out of 8
                    • First post
                      1/8
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved