Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. QRegExp. Extract sentence containing keyword to the end
Forum Updated to NodeBB v4.3 + New Features

QRegExp. Extract sentence containing keyword to the end

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 2 Posters 1.1k 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.
  • mandruk1331M Offline
    mandruk1331M Offline
    mandruk1331
    wrote on last edited by mandruk1331
    #1

    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?

    Mandruk1331

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      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.

      1 Reply Last reply
      4
      • mandruk1331M Offline
        mandruk1331M Offline
        mandruk1331
        wrote on last edited by mandruk1331
        #3

        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

        Mandruk1331

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved