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. How to split a string until nth space?
Forum Updated to NodeBB v4.3 + New Features

How to split a string until nth space?

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 4.4k Views 5 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.
  • K Offline
    K Offline
    kahlenberg
    wrote on 8 Feb 2017, 09:22 last edited by
    #1

    Hi,
    I have following string:

    QString str = "DRIVER_STAT.LEG1_LO_ERR         00000004	00000000	00000001	00000000	RO	LEG1 Driver Low Error";
    

    There are spaces and tabs between words and there are always 7 fields. I want to save them to a QStrginList but not affecting the last sentence, i. e. the desctiption "LEG1 Driver Low Error" must be one string. It shoult look like this:

    ["DRIVER_STAT.LEG1_LO_ERR"], ["00000004"], ["00000000"], ["00000001"], ["00000000"], ["RO"], ["LEG1 Driver Low Error"]
    

    How can I do that? What is the correct Regex for that?
    Thanks.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 8 Feb 2017, 09:34 last edited by
      #2

      Hi
      Are any of the fields fixed size?

      Also ["LEG1 Driver Low Error"]

      Can contain both space and/or tabs?

      So we do not know what separator is used - it can be mixed?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kahlenberg
        wrote on 8 Feb 2017, 09:39 last edited by
        #3

        Hi,
        Thanks for reply.
        To explain more clearly, the seperator is normaly tab character except the first word:

        "DRIVER_STAT.LEG1_LO_ERR(\s+)00000004(\t+)00000000(\t+)00000001(\t+)00000000(\t+)RO(\t+)LEG1(\s+)Driver(\s+)Low(\s+)Error"
        
        T 1 Reply Last reply 8 Feb 2017, 10:09
        1
        • K kahlenberg
          8 Feb 2017, 09:39

          Hi,
          Thanks for reply.
          To explain more clearly, the seperator is normaly tab character except the first word:

          "DRIVER_STAT.LEG1_LO_ERR(\s+)00000004(\t+)00000000(\t+)00000001(\t+)00000000(\t+)RO(\t+)LEG1(\s+)Driver(\s+)Low(\s+)Error"
          
          T Offline
          T Offline
          the_
          wrote on 8 Feb 2017, 10:09 last edited by
          #4

          @kahlenberg said in How to split a string until nth space?:

          "DRIVER_STAT.LEG1_LO_ERR(\s+)00000004(\t+)00000000(\t+)00000001(\t+)00000000(\t+)RO(\t+)LEG1(\s+)Driver(\s+)Low(\s+)Error"
          

          Means, that the after the first group the separators could be any number of whitespace characters or blanks only? As far as I remember \s matches any whitespace char

          • A space character
          • A tab character
          • A carriage return character
          • A new line character
          • A vertical tab character
          • A form feed character

          -- No support in PM --

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kahlenberg
            wrote on 8 Feb 2017, 10:15 last edited by
            #5

            Sory, I meant only one or more space character with \s+ and only one or more tab character with \t+. There are no \r, \n characters, There are only spaces or tabs.

            1 Reply Last reply
            0
            • G Offline
              G Offline
              Gerd
              wrote on 8 Feb 2017, 10:29 last edited by
              #6

              Hi,
              you can try this:

                 QStringList l1 = str.split('\t', QString::SkipEmptyParts);
                 QStringList l2 = l1.takeFirst().split(' ', QString::SkipEmptyParts);
                 l2.append(l1);
              
              
              1 Reply Last reply
              3
              • K Offline
                K Offline
                kahlenberg
                wrote on 8 Feb 2017, 10:57 last edited by
                #7

                Wow!
                Thank you very much. It works!

                T 1 Reply Last reply 8 Feb 2017, 11:16
                0
                • K kahlenberg
                  8 Feb 2017, 10:57

                  Wow!
                  Thank you very much. It works!

                  T Offline
                  T Offline
                  the_
                  wrote on 8 Feb 2017, 11:16 last edited by
                  #8

                  @kahlenberg

                  You could als try with a RegularExpression like this:

                  str = str.replace(QRegularExpression("[ ]+(.*$)"),"\t\\1").replace(QRegularExpression("[ ]+")," ")
                  QStringList list = str.split(QRegularExpression("\t+"));
                  

                  This will also remove multiple spaces in the last text block

                  -- No support in PM --

                  K 1 Reply Last reply 8 Feb 2017, 12:01
                  3
                  • T the_
                    8 Feb 2017, 11:16

                    @kahlenberg

                    You could als try with a RegularExpression like this:

                    str = str.replace(QRegularExpression("[ ]+(.*$)"),"\t\\1").replace(QRegularExpression("[ ]+")," ")
                    QStringList list = str.split(QRegularExpression("\t+"));
                    

                    This will also remove multiple spaces in the last text block

                    K Offline
                    K Offline
                    kshegunov
                    Moderators
                    wrote on 8 Feb 2017, 12:01 last edited by kshegunov 2 Aug 2017, 12:11
                    #9
                    QStringList list = str.split(QRegularExpression("\s+(?=[0-9]|RO|LEG1)"));
                    

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    3

                    1/9

                    8 Feb 2017, 09:22

                    • Login

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