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

QRegExp. Extract sentence containing keyword to the end

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 2 Posters 841 Views
  • 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.
  • M Offline
    M Offline
    mandruk1331
    wrote on 8 Oct 2018, 16:13 last edited by mandruk1331 10 Aug 2018, 16:14
    #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
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 8 Oct 2018, 20:09 last edited by Chris Kawa 10 Sept 2018, 10:13
      #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
      • M Offline
        M Offline
        mandruk1331
        wrote on 9 Oct 2018, 07:44 last edited by mandruk1331 10 Sept 2018, 07:54
        #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

        1/3

        8 Oct 2018, 16:13

        • Login

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