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. [SOLVED] Can't understand how to work with QRegExp
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Can't understand how to work with QRegExp

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 2.5k 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.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    I have in a qstring this:
    @R836044429,20120912,20120921,28
    R497476295,20120916,20120921,16@

    i want to separate each one that has coma and for example qdebug them in a new line..like
    @R836044429
    20120912
    20120921
    28
    R497476295
    20120916
    20120921
    16@

    i have read a lot of times the documentation but i just lose it in a point.. what have i done until now..

    @QTextStream in(&qstringnamehere);
    while(!in.atEnd())
    {
    QString line=in.readLine();

            QRegExp regex("R.*,");
            regex.setMinimal(true);
    
            QString match;
            int pos = 0;
    
            while ((pos = regex.indexIn(line, pos)) != -1)
            {
                match=regex.cap(0).remove(QChar(' '),Qt::CaseInsensitive);
                pos += regex.matchedLength();
            }
            if(match.isEmpty()){
                return;
            }
            QString current=match;
            current.replace(",","");
            qDebug(QString(current+"\n").toLocal8Bit());
    
            QRegExp regex2(",.*,");
            regex2.setMinimal(true);
    
            QString match2;
            pos = 0;
    
            while ((pos = regex2.indexIn(line, pos)) != -1)
            {
                match2=regex2.cap(0).remove(QChar(' '),Qt::CaseInsensitive);
                pos += regex2.matchedLength();
            }
            if(match2.isEmpty()){
                return;
            }
            QString current2=match2;
            current2.replace(",","");
            qDebug(QString(current2+"\n").toLocal8Bit());
        }@
    

    what am i doing wrong?
    p.s i don't know how to say from it to search from the 3rd coma until the end of the line
    i don't know when the captured texts are more than one how to separate them :/

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      You don't need a QRegExp for that. Just use QString::split and split on the comma.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Leon
        wrote on last edited by
        #3

        yes but this will make
        @28
        R497476295@

        as one because they are not separated by a coma..

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          Ok, so you split on both a line break or a comma. The documentation of QString::split has a nice example of using a very simple RegExp expression for something like this, that should work for you as well.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            Leon
            wrote on last edited by
            #5

            This seems to work for me..

            @ QStringList list = current_clipboard.split(QRegExp("\W+"), QString::SkipEmptyParts);
            @

            because \W stands for a non-word character :)
            Thanks Andre!

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andre
              wrote on last edited by
              #6

              Yes, that is the one I was eyeing :-)

              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