Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Find any things except one words by QRegularExpression
Qt 6.11 is out! See what's new in the release blog

Find any things except one words by QRegularExpression

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 1.2k 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.
  • thamT Offline
    thamT Offline
    tham
    wrote on last edited by tham
    #1

    String want to parse

    //there are many similar strings in the file, this is one of them
    <div class="rg_meta">{"id":"IuDOvhrPwcs2WM:","isu":"healthline.com","itg":0,"ity":"jpg","oh":728,"ou":"http://www.healthline.com/hlcmsresource/images/News/general-health/071916_unitsmoking_BODY.jpg","ow":1296,"pt":"Secondhand Smoke
    

    Expected result

    <div class="rg_meta">{"id":"IuDOvhrPwcs2WM:","isu":"healthline.com","itg":0,"ity":"jpg","oh":728,"ou"
    

    Real result is no match

    My solution

    QRegularExpression re("<div class=\"rg_meta\">{^(?!\"ou\").*\"ou\"");
    auto iter = re.globalMatch(contents);
    while(iter.hasMatch()){
      auto match = iter.next();
      qDebug()<<match.match(0);
    }else{
      qDebug()<<"no match";
    }
    

    How should I fix this error?Thanks

    1 Reply Last reply
    0
    • thamT Offline
      thamT Offline
      tham
      wrote on last edited by tham
      #2

      There are work around for my requirement, my ultimate goal is get the images address(ou and tu)

      QRegularExpression reg("<div class=\"rg_meta\">{[^}]*}");
      auto iter = reg.globalMatch(contents);        
      while(iter.hasNext()){  
          QRegularExpressionMatch match = iter.next();
          QRegularExpression link_big("\"ou\":\"([^\"]*)");
          QRegularExpression link_small("\"tu\":\"([^\"]*)");
          auto const bm = link_big.match(match.captured(0));
          auto const sm = link_small.match(match.captured(0));
          qDebug()<<bm.captured(0)<<","<<sm.captured(0);
      }
      

      This solution work, but I want to know why look ahead solution fail, how could I do it correctly in Qt?Thanks

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by SGaist
        #3

        Hi,

        Shouldn't it rather be something like <div class=\"rg_meta\".+(?>"ou") or <div class=\"rg_meta\">[a-zA-Z0-9{":,.\/]+(?>"ou") ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • thamT Offline
          thamT Offline
          tham
          wrote on last edited by
          #4

          Thanks, you are right, I have wrong understanding about negative look ahead of regex.

          Looks like there are not easy way to express

          match some string > match anything but a pattern
          

          by regular expression. The "work around" I use maybe a nice choice for this problem.

          1 Reply Last reply
          0
          • thamT Offline
            thamT Offline
            tham
            wrote on last edited by
            #5

            I mark this post as solved, if anybody come out better solution, please post at here, thanks

            1 Reply Last reply
            0

            • Login

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