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. Qstring: How to use regexp to slip words every two characters.
Forum Updated to NodeBB v4.3 + New Features

Qstring: How to use regexp to slip words every two characters.

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.9k Views 3 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.
  • D Offline
    D Offline
    doodle
    wrote on last edited by
    #1

    E.p slip QString("123456") into ("12", "34", "56");
    and slip QString("1234567") into (“12”, "34", "56", "7")

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      Hi
      If its always every two char then you can just loop over it.

      
        QString Input = "1234567";
        QString Out;
        for (int c = 0; c < Input.length(); ++c) {
          Out += Input[c];
          if ( c % 2)
            Out += " ";
        }
      
        qDebug() << Out;
      
      "12 34 56 7"
      
      
      D 1 Reply Last reply
      3
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by Paul Colby
        #3

        No sure why you want to use regular expressions (@mrjj 's solution would be faster), but...

        You can't use QString::split() because those methods take a separator, and you have none. So you'd have to use QRegularExpression::globalMatch() instead, like:

        QStringList parts;
        QRegularExpressionMatchIterator i = QRegularExpression("..?").globalMatch(QString("1234567"));
        while (i.hasNext()) {
           parts << i.next().captured(0);
        }
        qDebug() << parts;
        

        Outputs:

        ("12", "34", "56", "7")
        

        But yeah, for such a simple case, some simple index-based loop, like @mrjj suggested, would be much more efficient.

        Cheers.

        D 1 Reply Last reply
        3
        • mrjjM mrjj

          Hi
          If its always every two char then you can just loop over it.

          
            QString Input = "1234567";
            QString Out;
            for (int c = 0; c < Input.length(); ++c) {
              Out += Input[c];
              if ( c % 2)
                Out += " ";
            }
          
            qDebug() << Out;
          
          "12 34 56 7"
          
          
          D Offline
          D Offline
          doodle
          wrote on last edited by
          #4

          @mrjj Is it possible use regexp to make it?

          mrjjM 1 Reply Last reply
          0
          • D doodle

            @mrjj Is it possible use regexp to make it?

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @doodle
            @Paul-Colby version is the regexp version.
            Even more verbose I think its more solid
            as input can be much more different and it still works :)

            1 Reply Last reply
            0
            • Paul ColbyP Paul Colby

              No sure why you want to use regular expressions (@mrjj 's solution would be faster), but...

              You can't use QString::split() because those methods take a separator, and you have none. So you'd have to use QRegularExpression::globalMatch() instead, like:

              QStringList parts;
              QRegularExpressionMatchIterator i = QRegularExpression("..?").globalMatch(QString("1234567"));
              while (i.hasNext()) {
                 parts << i.next().captured(0);
              }
              qDebug() << parts;
              

              Outputs:

              ("12", "34", "56", "7")
              

              But yeah, for such a simple case, some simple index-based loop, like @mrjj suggested, would be much more efficient.

              Cheers.

              D Offline
              D Offline
              doodle
              wrote on last edited by
              #6

              @Paul-Colby Thanks!
              I need to process some Qstring like this " 12 ab, 1a 2ab f 12efa ", and I want to convert it to QStringList like this ("12", "ab", "1a", "2a", "b", "f", "12", "ef", "a").

              QString string("  12 ab, 1a, 2ab f 12efa    ");
                  QStringList list;
                  list = string.split(QRegularExpression("[\\s|,]"), QString::SkipEmptyParts);
              

              Outputs:

              ("12", "ab", "1a", "2ab", "f", "12efa")
              

              So if the regexp is able to split every two , I can make it just with

              QString::split
              
              Paul ColbyP 1 Reply Last reply
              1
              • D doodle

                @Paul-Colby Thanks!
                I need to process some Qstring like this " 12 ab, 1a 2ab f 12efa ", and I want to convert it to QStringList like this ("12", "ab", "1a", "2a", "b", "f", "12", "ef", "a").

                QString string("  12 ab, 1a, 2ab f 12efa    ");
                    QStringList list;
                    list = string.split(QRegularExpression("[\\s|,]"), QString::SkipEmptyParts);
                

                Outputs:

                ("12", "ab", "1a", "2ab", "f", "12efa")
                

                So if the regexp is able to split every two , I can make it just with

                QString::split
                
                Paul ColbyP Offline
                Paul ColbyP Offline
                Paul Colby
                wrote on last edited by
                #7

                @doodle said in Qstring: How to use regexp to slip words every two characters.:

                I need to process some Qstring like this " 12 ab, 1a 2ab f 12efa ", and I want to convert it to QStringList like this ("12", "ab", "1a", "2a", "b", "f", "12", "ef", "a").

                QStringList parts;
                QRegularExpressionMatchIterator i = QRegularExpression("[^\\s,]{1,2}").globalMatch(QString(" 12 ab, 1a 2ab f 12efa "));
                while (i.hasNext()) {
                   parts << i.next().captured(0);
                }
                qDebug() << parts;
                

                Output:

                ("12", "ab", "1a", "2a", "b", "f", "12", "ef", "a")
                

                Cheers.

                1 Reply Last reply
                3
                • D Offline
                  D Offline
                  doodle
                  wrote on last edited by
                  #8

                  @Paul-Colby @mrjj Thanks!

                  1 Reply Last reply
                  1

                  • Login

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