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] How to tell QRegExp to take the first result?
Qt 6.11 is out! See what's new in the release blog

[SOLVED] How to tell QRegExp to take the first result?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.7k 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.
  • L Offline
    L Offline
    Leon
    wrote on last edited by
    #1

    lets say we have a text

    @start wekjrnwekjrnkjn end erweriuweirbiwur end
    @

    This code

    @QRegExp filter("start(.+)end");
    int result = filter.indexIn(html_source);
    if(result != -1)
    qDebug() << filter.cap(1);@

    will output
    @wekjrnwekjrnkjn end erweriuweirbiwur@

    and not
    @wekjrnwekjrnkjn@

    that i want.. any ideas?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      Regular Expressions are "greedy" be default. The string it picks perfectly matches your RegExp. The string you want matches as well, but it's shorter. So RegExp picks to longer one. That's the greediness.

      The best way is to fix your RegExp, so it will only match the desired string.

      For example:
      @QRegExp filter("start\s+(\w+)\s+end");@

      Alternatively, you may try the QRegExp::setMinimal() function.

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • L Offline
        L Offline
        Leon
        wrote on last edited by
        #3

        [quote author="MuldeR" date="1378476582"]
        Alternatively, you may try the QRegExp::setMinimal() function.[/quote]

        Thank you :)
        exactly what i was looking for.
        should have looked better the documentation

        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