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



  • 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 :/



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



  • yes but this will make
    @28
    R497476295@

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



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



  • 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!



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.