QRegExp. Extract sentence containing keyword to the end



  • Hello,

    I have a text and would like to extract all of the strings containing a keyword until the end of the sentence.
    Here what I have came up with:
    Sample text:

    sddadsadsadsa: [error] dsadsdasdsads
    sddadsadsadsa: [error] dsadsdasdsads
    sdadsadadsad
    sddadsadsadsa: [error] dsadsdasdsads
    

    Code:

       QRegExp rx;
       rx.setPattern("\[error].*");
       rx.setPatternSyntax(QRegExp::RegExp);
       rx.indexIn(data);
       QStringList list = rx.capturedTexts();
    

    I have tried using this syntax in Notepad++ and everything worked nicely. But here I am getting an empty string list. Could you tell me what am I doing wrong?


  • Moderators

    It's recommended to use QRegularExpression instead of QRegExp in new code.
    Notepad++ automatically applies the pattern to each line. In Qt you need to do that yourself. You can use a handy iterator returned by globalMatch() function for that.
    \ is an escape character in C++. If you want to use that character in a string you need to escape it i.e. double it \\ or use the C++ raw string literal.
    If you want to get the text after a keyword you need to actually capture it i.e. use capture group (.*).

    So a code for your example could look like this:

    QRegularExpression rx(R"(.*[error](.*))");
    auto it = rx.globalMatch(data);
    while(it.hasNext())
    {
        auto match = it.next();
        if (match.lastCapturedIndex() == 1)
            qDebug() << match.capturedView(1);
    }
    

    or

    QRegularExpression rx(".*\\[error\\](.*)");
    

    if you don't want to use the raw string literal.



  • Hello,

    I have tried your variant it returns string which I do not need. I am parsing out an HTML code of the page. When I run you variant it returns all kind of string, and I only need the sentence where the "[error]" key word is present.

    Update:
    This variant of RegExp works like a charm:

    \\[error\\](.*)
    

    Thank you for your time. The issue has been resolved


Log in to reply