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. Getting an index of match using QRegExp.
Forum Updated to NodeBB v4.3 + New Features

Getting an index of match using QRegExp.

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.0k 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.
  • P Offline
    P Offline
    piotrekd
    wrote on last edited by
    #1

    Hello, when I tried to write an simple module to remove multi-line comments from cpp file I've got a problem with getting an index of begin and end of comment section.

    I merged the whole of file into one QString called "text" in code below.
    @
    QRegExp rx = QRegExp("\/\"); // matches begin of comment section
    QRegExp rx2 = QRegExp("\
    \/"); // matches end of comment section

    qDebug() << rx.indexIn(this->text); // returns corrrectly one, first index but I need all of them so I think I need to use code below
    qDebug() << rx.pos(1);          // returns -1 so It didn't match
    qDebug() << rx.pos(2);        // same here
    

    @

    Sample output:
    @

    Starting /home/piotrek/dev/qt/scs_finder-build-desktop/scs_finder...

    "#include <boost/lambda/lambda.hpp> /* Sample / #include <iostream> #include <iterator> #include <algorithm> int main() { using namespace boost::lambda; typedef std::istream_iterator<int> in; / Annother sample */ std::for_each( in(std::cin), in(), std::cout << (_1 * 3) << " " ); } "

    35
    -1
    -1
    @

    I'll be very happy if someone could give me a tip how to resolve this problem.
    Greetings,
    Piotrek

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Richard Neal
      wrote on last edited by
      #2

      Since your using QString, something like this maybe?

      @#include <QtCore/QRegExp>
      #include <QtCore/QDebug>

      int main()
      {
      QString s1 = "1: Some code commented /* this is a comment / more code";
      //QString s1 = "1: Some code commented /
      this is a comment more code";

      int start = s1.indexOf( "/*" );
      int end = s1.indexOf( "*/" );
      
      if( end > start ){
          QString tmpStr;
          for( int i = start; i <= end+1; ++i ){
              tmpStr.append( s1[i] );
          }
          qDebug() << tmpStr;
      } else {
          qDebug() << "Invalid comment found:\n\t" << s1;
      }
      

      }@

      For every positive is a negative and every negative is a positive.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dangelog
        wrote on last edited by
        #3

        [quote author="Richard Neal" date="1314968851"]
        @
        QString tmpStr;
        for( int i = start; i <= end+1; ++i ){
        tmpStr.append( s1[i] );
        }
        @
        [/quote]

        QString::mid().

        Software Engineer
        KDAB (UK) Ltd., a KDAB Group company

        1 Reply Last reply
        0
        • R Offline
          R Offline
          Richard Neal
          wrote on last edited by
          #4

          Sure - (would need to calculate length you want from pos in that case).

          Like:
          @#include <QtCore/QRegExp>
          #include <QtCore/QDebug>

          int main()
          {
          QString s1 = "1: Some code commented /* this is a comment / more code";
          //QString s1 = "1: Some code commented /
          this is a comment more code";

          int start = s1.indexOf( "/*" );
          int end = s1.indexOf( "*/" );
          
          if( end > start ){
              qDebug() << s1.mid( start, end-start+2 );
          } else {
              qDebug() << "Invalid comment found:\n\t" << s1;
          }
          

          }@

          For every positive is a negative and every negative is a positive.

          1 Reply Last reply
          0
          • P Offline
            P Offline
            piotrekd
            wrote on last edited by
            #5

            Thanks for all replies, that is what I needed :)

            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