[SOLVED] How to tell QRegExp to take the first result?

  • lets say we have a text

    @start wekjrnwekjrnkjn end erweriuweirbiwur end

    This code

    @QRegExp filter("start(.+)end");
    int result = filter.indexIn(html_source);
    if(result != -1)
    qDebug() << filter.cap(1);@

    will output
    @wekjrnwekjrnkjn end erweriuweirbiwur@

    and not

    that i want.. any ideas?

  • Regular Expressions are "greedy" be default. The string it picks perfectly matches your RegExp. The string you want matches as well, but it's shorter. So RegExp picks to longer one. That's the greediness.

    The best way is to fix your RegExp, so it will only match the desired string.

    For example:
    @QRegExp filter("start\s+(\w+)\s+end");@

    Alternatively, you may try the QRegExp::setMinimal() function.

  • [quote author="MuldeR" date="1378476582"]
    Alternatively, you may try the QRegExp::setMinimal() function.[/quote]

    Thank you :)
    exactly what i was looking for.
    should have looked better the documentation

Log in to reply

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