[ANOTHER QUESTION] Can anyone explain me QRegExp?



  • Yes i have seen the documentation but it is kinda hard to understand.. I would like an explanation...
    Lets say we have

    @ QString leon="1\\\\\\\\n2\\\\\\\\n3\\\\\\\\ni want only the next line\\\\\\\\nfrom this line i want only --->Hello guys!<---\\\\\\\\nend here i dont want it";
    @

    how can i get the Hello guys! only?

    so the code that must be used is something like

    @ QRegExp regex("time=.* ");
    regex.setMinimal(true);

        QStringList list;
        int pos = 0;
    
        while ((pos = regex.indexIn(leon, pos)) != -1)
        {
            list << regex.cap(0).remove(QChar(' '),Qt::CaseInsensitive);
            pos += regex.matchedLength();
        }
        if(!list.count()){
            //error
            return;
        }
         QString current=list.at(0);
        qDebug(current.toLocal8Bit());
    

    @

    i don't get what should i put in line 1 in the parentheses so the qdebug show Hello guys!



  • something like @"--->()<---*"@ i guess



  • Regular expressions are powerful but require some love :). There are many engines with similar but not exactly the same future sets. For example, Qt 4 doesn't support look behinds.

    Personally, I liked tutorial on "this":http://www.regular-expressions.info/tutorial.html page the most. For testing, there are many online RegEx testers like "this one":http://www.rubular.com/.

    Now, back to your problem. Due to lack of look behinds, only reasonable way to extract "Hello guys" string I can see are following two steps:
    @
    QString leon="1\\n2\\n3\\ni want only the next line\\nfrom this line i want only --->Hello guys!<---\\nend here i dont want it";
    QRegExp regex("--->.*(?=<---)");
    if(regex.indexIn(leon) != -1){
    QStringList list;
    QString str;
    list = regex.capturedTexts();
    foreach(str,list)
    qDebug() << str.remove("--->");
    }
    @

    It is very likely someone will post one step regex only solution soon.

    Since there are 2 steps anyway you could simply use QString::remove() to remove everything before and including ---> and after and including <---.
    I'm not sure if there are significant difference in speed between 2 removes, or regex + remove version, but you can always benchmark it :).



  • [quote author="sidewinder" date="1343376961"]Regular expressions are powerful but require some love :). There are many engines with similar but not exactly the same future sets. For example, Qt 4 doesn't support look behinds.

    Personally, I liked tutorial on "this":http://www.regular-expressions.info/tutorial.html page the most. For testing, there are many online RegEx testers like "this one":http://www.rubular.com/.

    Now, back to your problem. Due to lack of look behinds, only reasonable way to extract "Hello guys" string I can see are following two steps:
    @
    QString leon="1\\\\n2\\\\n3\\\\ni want only the next line\\\\nfrom this line i want only --->Hello guys!<---\\\\nend here i dont want it";
    QRegExp regex("--->.*(?=<---)");
    if(regex.indexIn(leon) != -1){
    QStringList list;
    QString str;
    list = regex.capturedTexts();
    foreach(str,list)
    qDebug() << str.remove("--->");
    }
    @

    It is very likely someone will post one step regex only solution soon.

    Since there are 2 steps anyway you could simply use QString::remove() to remove everything before and including ---> and after and including <---.
    I'm not sure if there are significant difference in speed between 2 removes, or regex + remove version, but you can always benchmark it :).[/quote]

    Thanks a lot! It works, but i would like an explanation for line 2.. what are
    @.*(?=@
    i quess they remove <-- but why not do something similar for the -->?



  • Step by step.
    @"--->"@
    matches "--->" :)

    @.@
    matches any number (
    ) of any characters (.)

    @(?=)@
    is called look ahead. It means "check if there is pattern ahead, but don't include it in results".

    So
    @.*(?=<---)@
    checks if after any number of any characters <--- is present.

    Qt doesn't support look behinds (?<=), so unfortunately something like that doesn't work:
    @(?<=--->).*(?=<---)@

    Hopefully it will work in Qt5 :).



  • Ok got it thanks ! :)


  • Moderators

    [quote author="sidewinder" date="1343388065"]Hopefully it will work in Qt5 :).[/quote]

    Yes, but not without code changes. QRegExp was entangled too much into Qt code, it had to be left in Qt5. To solve regexp issues, a new class was introduced - "QRegularExpression":http://doc-snapshot.qt-project.org/5.0/qregularexpression.html. It uses a third party regexp engine that supports everything you might need and is also much faster :D



  • What if i only want to show the first capture?
    For example

    @QString lool="1/n3/n5/n6/n7/3/n5/n9";
    QRegExp reg2("3.*5");
    if(reg2.indexIn(lool) != -1){
    QStringList list;
    QString strq;
    list = reg2.capturedTexts();
    foreach(strq,list)
    qDebug(strq.toLocal8Bit());
    }@

    it will show
    @3/n5/n6/n7/3/n5@

    while i want from it to show
    @3/n5@



  • Anyone? Sorry for 2nd post..



  • I can say that I have quite the same problem here... I want only the first occurrence of the match!



  • You have to turn off the 'greedy' mode for this... see "here":http://qt-project.org/doc/qt-4.8/qregexp.html#setMinimal


Log in to reply
 

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