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. Spliting a text file by according to a delimiter
Forum Updated to NodeBB v4.3 + New Features

Spliting a text file by according to a delimiter

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 940 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
    Mihic
    wrote on last edited by
    #1

    I'm building an app that has an autocomplete text functionality.
    What I'm trying to implement, but having trouble finding the solution for anywhere, is reading a block of text until a character set as a delimiter, i.e. reading a variable block size of text and iterating through it.
    If we take "$" as a delimiter character, the example would be:

    
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum imperdiet massa leo, id auctor metus placerat sit amet. Nam semper nisl in diam feugiat laoreet.
    
    $
    
    Etiam ut quam dignissim, scelerisque urna sed, imperdiet lacus. Nulla facilisi. Phasellus fringilla augue a ex tristique, gravida suscipit nibh auctor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Curabitur lacinia ac metus eget facilisis. Praesent in lacinia leo. Suspendisse a nunc at enim tempus pharetra ac in libero. Sed lacinia lobortis erat et pellentesque. Curabitur scelerisque magna sit amet nisl scelerisque, non volutpat mi molestie. Vestibulum bibendum et magna sed dictum. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    
    $
    
    Vestibulum congue, dolor quis faucibus volutpat
    
    $
    
    ~~~~ end of text file ~~~~
    
    My question is, is there any way to parse this text file in a way that the text in between the delimiter characters is read, no matter the text block size.
    
    I'm working in Qt C++
    Thanks in advance and sorry for my bad english.
    jsulmJ JonBJ 2 Replies Last reply
    0
    • M Mihic

      I'm building an app that has an autocomplete text functionality.
      What I'm trying to implement, but having trouble finding the solution for anywhere, is reading a block of text until a character set as a delimiter, i.e. reading a variable block size of text and iterating through it.
      If we take "$" as a delimiter character, the example would be:

      
      Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum imperdiet massa leo, id auctor metus placerat sit amet. Nam semper nisl in diam feugiat laoreet.
      
      $
      
      Etiam ut quam dignissim, scelerisque urna sed, imperdiet lacus. Nulla facilisi. Phasellus fringilla augue a ex tristique, gravida suscipit nibh auctor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Curabitur lacinia ac metus eget facilisis. Praesent in lacinia leo. Suspendisse a nunc at enim tempus pharetra ac in libero. Sed lacinia lobortis erat et pellentesque. Curabitur scelerisque magna sit amet nisl scelerisque, non volutpat mi molestie. Vestibulum bibendum et magna sed dictum. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      
      $
      
      Vestibulum congue, dolor quis faucibus volutpat
      
      $
      
      ~~~~ end of text file ~~~~
      
      My question is, is there any way to parse this text file in a way that the text in between the delimiter characters is read, no matter the text block size.
      
      I'm working in Qt C++
      Thanks in advance and sorry for my bad english.
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Mihic Simply read the text into a QString variable and then split it using your delimiter (https://doc.qt.io/qt-6/qstring.html#split-1).

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • M Mihic

        I'm building an app that has an autocomplete text functionality.
        What I'm trying to implement, but having trouble finding the solution for anywhere, is reading a block of text until a character set as a delimiter, i.e. reading a variable block size of text and iterating through it.
        If we take "$" as a delimiter character, the example would be:

        
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum imperdiet massa leo, id auctor metus placerat sit amet. Nam semper nisl in diam feugiat laoreet.
        
        $
        
        Etiam ut quam dignissim, scelerisque urna sed, imperdiet lacus. Nulla facilisi. Phasellus fringilla augue a ex tristique, gravida suscipit nibh auctor. Interdum et malesuada fames ac ante ipsum primis in faucibus. Curabitur lacinia ac metus eget facilisis. Praesent in lacinia leo. Suspendisse a nunc at enim tempus pharetra ac in libero. Sed lacinia lobortis erat et pellentesque. Curabitur scelerisque magna sit amet nisl scelerisque, non volutpat mi molestie. Vestibulum bibendum et magna sed dictum. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        
        $
        
        Vestibulum congue, dolor quis faucibus volutpat
        
        $
        
        ~~~~ end of text file ~~~~
        
        My question is, is there any way to parse this text file in a way that the text in between the delimiter characters is read, no matter the text block size.
        
        I'm working in Qt C++
        Thanks in advance and sorry for my bad english.
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @Mihic said in Spliting a text file by according to a delimiter:

        My question is, is there any way to parse this text file in a way that the text in between the delimiter characters is read, no matter the text block size.

        @jsulm proposes a simple solution. If, say, your text file is 4GB+ big, with frequent $ lines dividing it into a lot of small "chunks", you will use an awful lot of memory reading the whole file into a QString and splitting it. Is that acceptable to you as a solution, or do you want/need something with a much smaller memory footprint?

        jsulmJ 1 Reply Last reply
        3
        • JonBJ JonB

          @Mihic said in Spliting a text file by according to a delimiter:

          My question is, is there any way to parse this text file in a way that the text in between the delimiter characters is read, no matter the text block size.

          @jsulm proposes a simple solution. If, say, your text file is 4GB+ big, with frequent $ lines dividing it into a lot of small "chunks", you will use an awful lot of memory reading the whole file into a QString and splitting it. Is that acceptable to you as a solution, or do you want/need something with a much smaller memory footprint?

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @JonB Yes.
          @Mihic Alternative would be to read the file line by line and accumulate lines until you get a line containing the delimiter character.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          4
          • M Offline
            M Offline
            Mihic
            wrote on last edited by
            #5

            The solution is acceptable since the file is no bigger than 100KB, there's only one thing I'm still having trouble with, and that is defining the regular expression itself. If you wouldn't mind writing the example I'm looking for with the "$" sign. When i split the string what should i put in the QRegularExpression brackets so that the string is split according to the "$" sign.
            Thank you again and sorry for bothering you.

            jsulmJ JonBJ 2 Replies Last reply
            0
            • M Mihic

              The solution is acceptable since the file is no bigger than 100KB, there's only one thing I'm still having trouble with, and that is defining the regular expression itself. If you wouldn't mind writing the example I'm looking for with the "$" sign. When i split the string what should i put in the QRegularExpression brackets so that the string is split according to the "$" sign.
              Thank you again and sorry for bothering you.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Mihic Why do you need a regular expression? Just use the delimiter character as parameter for split() method.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • M Mihic

                The solution is acceptable since the file is no bigger than 100KB, there's only one thing I'm still having trouble with, and that is defining the regular expression itself. If you wouldn't mind writing the example I'm looking for with the "$" sign. When i split the string what should i put in the QRegularExpression brackets so that the string is split according to the "$" sign.
                Thank you again and sorry for bothering you.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @Mihic
                We do not know your exact rule about how you want to split "with the $ sign" you show. It might be any $ anywhere (doubtful), it might be $ at start of line or end of line, $ on a line of its own, $ preceded and followed by blank line, etc....

                Initially I would suggest don't both a regular expression, what about

                stringInput.split("$\\n");
                

                Does that do you?

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  Mihic
                  wrote on last edited by
                  #8

                  That solution works great, thank you both very much.

                  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