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. [SOLVED]QRegExp is too greedy...
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]QRegExp is too greedy...

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 2.5k 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.
  • T Offline
    T Offline
    ThaRez
    wrote on last edited by
    #1

    Hello
    I have a QString, "data" that contains the source of a web page. Included in this is the following line:

    @<iframe width="640" height="360" src="http://www.mysite.com/embed/4Ieelwhw" frameborder="0" allowfullscreen></iframe>@

    I'm trying to extract the path that the src attribute equals to using a regexp. What I'm doing is the following:

    @QRegExp regx("<iframe .src="(.)(".></iframe>)"); //<b>[^<]</b>
    regx.setMinimal(true);
    regx.indexIn(strType);

        qDebug() << strType.mid(regx.pos(1),regx.pos(2)-regx.pos(1));@
    

    My problem is that the regex is too "greedy" though the setMinimal is set to true... The output in the debug output is:

    "http://www.mysite.com/embed/4Ieelwhw" frameborder="0"

    How can I make it stop at the first " and not include the frameborder? Thanks

    -RS

    1 Reply Last reply
    0
    • JohanSoloJ Offline
      JohanSoloJ Offline
      JohanSolo
      wrote on last edited by
      #2

      I think the '?' character in your regex disables the greedy mode.

      `They did not know it was impossible, so they did it.'
      -- Mark Twain

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tobias.hunger
        wrote on last edited by
        #3

        !I have not tested this!

        You could also try: "src="([^"]*)".

        That is src=" followed by any character that is not a ".

        1 Reply Last reply
        0
        • T Offline
          T Offline
          ThaRez
          wrote on last edited by
          #4

          Unfortunately neither suggestion helped...

          I tried:

          @QRegExp regx("<iframe .src="([^\”])(".*></iframe>)");
          regx.setMinimal(true);
          regx.indexIn(strType);

              qDebug() << strType.mid(regx.pos(1),regx.pos(2)-regx.pos(1));@
          

          But the output is the same:

          “http://www.mysite.com/embed/4Ieelwhw” frameborder=“0”

          I also tried:
          @QRegExp regx("<iframe .src="(.?)(".></iframe>)");
          QRegExp regx("<iframe .src="(.?)(".
          ></iframe>)");@
          But neither matched anything...

          Any further suggestions? Thanks!
          -RS

          1 Reply Last reply
          0
          • T Offline
            T Offline
            ThaRez
            wrote on last edited by
            #5

            ok, found the problem (when I posted my reply) Apparently there was a copy paste issue: [^\”] vs [^"]*.

            it now works with:

            @QRegExp regx("<iframe .src="([^"])(".*></iframe>)");@

            Thank you!
            -RS

            1 Reply Last reply
            0
            • JohanSoloJ Offline
              JohanSoloJ Offline
              JohanSolo
              wrote on last edited by JohanSolo
              #6

              I would rather write (don't know whether it works though):

              QRegExp regx("&lt;iframe .*src=\"(.+?)(\".*&gt;&lt;/iframe>)");
              

              Note the + instead of your *. + stands for at least one character and maybe more, whilst * stands for zero or one character.

              [quote author="ThaRez" date="1340956318"]I also tried:
              @QRegExp regx("<iframe .src="(.?)(".></iframe>)");
              QRegExp regx("<iframe .src="(.?)(".
              ></iframe>)");@
              But neither matched anything...

              Any further suggestions? Thanks!
              -RS

              [/quote]

              `They did not know it was impossible, so they did it.'
              -- Mark Twain

              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