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. QRegularExpression need help with patern
Qt 6.11 is out! See what's new in the release blog

QRegularExpression need help with patern

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

    Hello all,

    I just can't get it right.. maybe someone can help me with it?

    I have QByteArray with list of elements in it and looks like that:

    @
    <!--story_23015_start--> test test 234 2tsdtsdf sf sdfds <!--story_23015_end-->
    ......
    <!--story_2301275_start--> tsdtsdf sf sdfds <!--story_2301275_end-->
    @

    how can I extract numbers between "<!--story_" & "_start-->" and text in the midle from each item using QRegularExpression?

    I will really appreciate your help!

    God is Real unless explicitly declared as Integer.

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

      The following pattern works (RegExp Example)

      @<!--story_(\d+)_start-->(.+)<!--story.+-->$@

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        AcerExtensa
        wrote on last edited by
        #3

        Thank you very much mcosta

        The problem is: items are not one per line and they can have text or any chars at the beginning or at the end. Like:
        @
        asdmpowq d34 23 <!--story_23015_start--> test test 234 2tsdtsdf sf sdfds <!--story_23015_end--> html tags etc
        <!--story_2301275_start--> tsdtsdf sf sdfds <!--story_2301275_end-->
        @

        I have tried this way, but I just whole items as one item and not the list.

        @
        QRegExp rx("(.+)<!--story_(\d+)_start-->(.+)<!--story.+-->(.+)");
        int pos = 0;
        while((pos = rx.indexIn(data,pos)) != -1)
        {
        qDebug() << rx.cap(2) << pos;
        pos += rx.matchedLength();
        }
        @

        God is Real unless explicitly declared as Integer.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          Hi,

          you could try to do this:

          • remove all newline
          • extract the signle tag
          • applu the regular expression

          Once your problem is solved don't forget to:

          • Mark the thread as SOLVED using the Topic Tool menu
          • Vote up the answer(s) that helped you to solve the issue

          You can embed images using (http://imgur.com/) or (http://postimage.org/)

          1 Reply Last reply
          0
          • TheBadgerT Offline
            TheBadgerT Offline
            TheBadger
            wrote on last edited by
            #5

            Hi,

            You can use an expression as follow:

            @
            <!--story_(\d+)start-->(.+)(?=<!--story(\d+)_end-->)
            @

            But I am not sure that the positive lookahead (?=) works on QRegExp, it should work according to the docs.

            According to the docs the .(dot) should match newlines.

            Hint: I normally use the following website to test my regular expressions, it works quite well: http://www.regexr.com/


            Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

            1 Reply Last reply
            0
            • TheBadgerT Offline
              TheBadgerT Offline
              TheBadger
              wrote on last edited by
              #6

              Another interesting option is (sorry just escape for usage):

              @
              <!--story_(\d+)start-->([\s\S]+)<!--story(\1)_end-->
              @

              This can be used to make sure that the story number matches in the start and end using a "backreference":http://qt-project.org/doc/qt-4.8/qregexp.html#backreferences to the number captured in the start.

              The [\s\S] is used if the .(dot) does not match a new line.

              If you don't want to capture the number in the end tag you can use the (?:\1) tag to make a non-capturing group for that number.

              Hope this helps a bit


              Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

              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