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. Problem with using RegExp

Problem with using RegExp

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 583 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.
  • S Offline
    S Offline
    Subuday
    wrote on last edited by
    #1

    I need to determine /hello.htm in string GET /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 \r\n using RegExp. I use

    \/.+?\s
    

    It wokrs fine on Online Regular Expression
    but in qt it doesn't

        QRegExp uriRegExp("\/.+?\s");
        int lastPos  = 0; 
               
         while((lastPos = uriRegExp.indexIn(myString, lastPos)) != -1)
        {
           qDebug() << uriRegExp.cap(1);
           lastPos += uriRegExp.matchedLength();
      }
    
    jsulmJ 1 Reply Last reply
    0
    • S Subuday

      I need to determine /hello.htm in string GET /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 \r\n using RegExp. I use

      \/.+?\s
      

      It wokrs fine on Online Regular Expression
      but in qt it doesn't

          QRegExp uriRegExp("\/.+?\s");
          int lastPos  = 0; 
                 
           while((lastPos = uriRegExp.indexIn(myString, lastPos)) != -1)
          {
             qDebug() << uriRegExp.cap(1);
             lastPos += uriRegExp.matchedLength();
        }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Subuday Change

      QRegExp uriRegExp("\/.+?\s");
      

      to

      QRegExp uriRegExp("\\/.+?\s");
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 1 Reply Last reply
      0
      • jsulmJ jsulm

        @Subuday Change

        QRegExp uriRegExp("\/.+?\s");
        

        to

        QRegExp uriRegExp("\\/.+?\s");
        
        S Offline
        S Offline
        Subuday
        wrote on last edited by
        #3

        @jsulm Doesn't work

        jsulmJ 1 Reply Last reply
        0
        • mranger90M Offline
          mranger90M Offline
          mranger90
          wrote on last edited by
          #4
          qDebug() << "Starting";
          QString testString("GET /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 \r\n ");
          QRegularExpression re("/.+?\\s");
          QRegularExpressionMatch match = re.match(testString);
          
          if (match.hasMatch())
          {
              QString matched = match.captured(0);
              qDebug() << "Matched " << matched;
          }
          else
          {
              qDebug() << "No match";
          }
          
          qDebug() << "done";
          
          S 1 Reply Last reply
          2
          • S Subuday

            @jsulm Doesn't work

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Subuday Should be

            QRegExp uriRegExp("\\/.+?\\s");
            

            \ is escape character in C++, if you want to use it as part of a string you have to escape it as \\

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • mranger90M mranger90
              qDebug() << "Starting";
              QString testString("GET /hello.htm HTTP/1.1 User-Agent: Mozilla/4.0 \r\n ");
              QRegularExpression re("/.+?\\s");
              QRegularExpressionMatch match = re.match(testString);
              
              if (match.hasMatch())
              {
                  QString matched = match.captured(0);
                  qDebug() << "Matched " << matched;
              }
              else
              {
                  qDebug() << "No match";
              }
              
              qDebug() << "done";
              
              S Offline
              S Offline
              Subuday
              wrote on last edited by
              #6

              @mranger90 Thank you!

              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