Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Split words in letters
Qt 6.12 LTS is out! See what's new in the release blog

Split words in letters

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 7.2k 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.
  • AlvaroSA AlvaroS

    Hi everyone.
    Thanks a lot for reading this post and helping me.

    I have a QStringList words which has the next:

    QStringList words_of_line
    

    words_of_line = [-10,30]

    Now I would like to get just the numbers so -10 and 30.
    The format will be always the same, some examples:
    [5,6] [10,-5] [-1,20]

    So how can I get just the numbers? (it it is negative I have to get the negative.)

    kshegunovK Offline
    kshegunovK Offline
    kshegunov
    Moderators
    wrote on last edited by kshegunov
    #3

    @AlvaroS
    You can indeed use split as @mrjj said, but you can also extract them directly with a regex (\[\s*(-?[0-9]+)\s*,\s*(-?[0-9]+)\s*\]). However the regex will probably work much better if you had a whole string.

    Read and abide by the Qt Code of Conduct

    1 Reply Last reply
    4
    • AlvaroSA AlvaroS

      Hi everyone.
      Thanks a lot for reading this post and helping me.

      I have a QStringList words which has the next:

      QStringList words_of_line
      

      words_of_line = [-10,30]

      Now I would like to get just the numbers so -10 and 30.
      The format will be always the same, some examples:
      [5,6] [10,-5] [-1,20]

      So how can I get just the numbers? (it it is negative I have to get the negative.)

      D Offline
      D Offline
      Devopia53
      wrote on last edited by
      #4

      @AlvaroS

      Also, you can use the capture method of regular expression.
      Something like this:

      QRegExp         rx("(-?\\d+),(-?\\d+)");
      
      foreach (auto str, QStringList() << "[5,6]" << "[10,-5]" << "[-1,20]") {
          if (rx.indexIn(str) > -1)
              qDebug() << "lvalue:" << rx.cap(1) << "rvalue:" << rx.cap(2);
      }
      
      AlvaroSA 1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #5

        Hi,

        To add to my fellow regexp fans. Use QRegularExpression with Qt 5. QRegExp has been deprecated.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        AlvaroSA 1 Reply Last reply
        4
        • mrjjM mrjj

          Hi
          You can use the split function

          http://doc.qt.io/qt-5/qstring.html#split

          AlvaroSA Offline
          AlvaroSA Offline
          AlvaroS
          wrote on last edited by
          #6

          @mrjj Thanks! I have solved with this! it is easy!!

          1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            To add to my fellow regexp fans. Use QRegularExpression with Qt 5. QRegExp has been deprecated.

            AlvaroSA Offline
            AlvaroSA Offline
            AlvaroS
            wrote on last edited by
            #7

            @SGaist hi! First of all thanks for answering!!
            It is a little hard for me. Where can I find the Regular expresion in C++? like -? d+. the meaning of that!

            Thanks a lot!

            1 Reply Last reply
            0
            • D Devopia53

              @AlvaroS

              Also, you can use the capture method of regular expression.
              Something like this:

              QRegExp         rx("(-?\\d+),(-?\\d+)");
              
              foreach (auto str, QStringList() << "[5,6]" << "[10,-5]" << "[-1,20]") {
                  if (rx.indexIn(str) > -1)
                      qDebug() << "lvalue:" << rx.cap(1) << "rvalue:" << rx.cap(2);
              }
              
              AlvaroSA Offline
              AlvaroSA Offline
              AlvaroS
              wrote on last edited by
              #8

              @Devopia53 said:

              @AlvaroS

              Also, you can use the capture method of regular expression.
              Something like this:

              QRegExp         rx("(-?\\d+),(-?\\d+)");
              
              foreach (auto str, QStringList() << "[5,6]" << "[10,-5]" << "[-1,20]") {
                  if (rx.indexIn(str) > -1)
                      qDebug() << "lvalue:" << rx.cap(1) << "rvalue:" << rx.cap(2);
              }
              

              @kshegunov said:

              @AlvaroS
              You can indeed use split as @mrjj said, but you can also extract them directly with a regex (\[\s*(-?[0-9]+)\s*,\s*(-?[0-9]+)\s*\]). However the regex will probably work much better if you had a whole string.

              Thanks for asnwering friends! Like I said in the last post. Where can I find the regular expressions of C++ like -? s*??? The meaning of that!

              Thanks!

              1 Reply Last reply
              0
              • AlvaroSA Offline
                AlvaroSA Offline
                AlvaroS
                wrote on last edited by
                #9

                hi again.

                Now I am trying split one word but I have to get the first paramenter. For example:

                i have this: /home/user/image.jpeg

                So I have to get just "/home/user/"
                So every characters until the last /.

                How can I do that?!

                Thanks!!

                kshegunovK 1 Reply Last reply
                0
                • AlvaroSA AlvaroS

                  hi again.

                  Now I am trying split one word but I have to get the first paramenter. For example:

                  i have this: /home/user/image.jpeg

                  So I have to get just "/home/user/"
                  So every characters until the last /.

                  How can I do that?!

                  Thanks!!

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #10

                  @AlvaroS said:

                  Where can I find the regular expressions of C++ like -? s*??? The meaning of that!

                  You can't because this is not part of C++. This is something Qt provides for you through the QRegularExpression class. That class is using PCRE (perl-compatible regular expressions), so you can see the syntax on any site that deals with them. For example:
                  http://www.pcre.org/pcre.txt
                  or more humanly presented:
                  http://php.net/manual/en/reference.pcre.pattern.syntax.php

                  Also you can experiment and test PCREs here:
                  https://regex101.com/ (select PCRE flavor in the left sidebar).

                  Now I am trying split one word but I have to get the first paramenter. For example:
                  i have this: /home/user/image.jpeg

                  For this, you're better of using Qt's file/dir/fileinfo classes. For the simplest of examples:

                  QString filePath = "/home/user/image.jpeg";
                  QString dirPath = QFileInfo(filePath).dir().path();
                  

                  Read and abide by the Qt Code of Conduct

                  AlvaroSA 1 Reply Last reply
                  3
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #11

                    You'll be also interested by the regular expression tool

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • kshegunovK kshegunov

                      @AlvaroS said:

                      Where can I find the regular expressions of C++ like -? s*??? The meaning of that!

                      You can't because this is not part of C++. This is something Qt provides for you through the QRegularExpression class. That class is using PCRE (perl-compatible regular expressions), so you can see the syntax on any site that deals with them. For example:
                      http://www.pcre.org/pcre.txt
                      or more humanly presented:
                      http://php.net/manual/en/reference.pcre.pattern.syntax.php

                      Also you can experiment and test PCREs here:
                      https://regex101.com/ (select PCRE flavor in the left sidebar).

                      Now I am trying split one word but I have to get the first paramenter. For example:
                      i have this: /home/user/image.jpeg

                      For this, you're better of using Qt's file/dir/fileinfo classes. For the simplest of examples:

                      QString filePath = "/home/user/image.jpeg";
                      QString dirPath = QFileInfo(filePath).dir().path();
                      
                      AlvaroSA Offline
                      AlvaroSA Offline
                      AlvaroS
                      wrote on last edited by
                      #12

                      @kshegunov said:

                      @AlvaroS said:

                      Where can I find the regular expressions of C++ like -? s*??? The meaning of that!

                      You can't because this is not part of C++. This is something Qt provides for you through the QRegularExpression class. That class is using PCRE (perl-compatible regular expressions), so you can see the syntax on any site that deals with them. For example:
                      http://www.pcre.org/pcre.txt
                      or more humanly presented:
                      http://php.net/manual/en/reference.pcre.pattern.syntax.php

                      Also you can experiment and test PCREs here:
                      https://regex101.com/ (select PCRE flavor in the left sidebar).

                      Now I am trying split one word but I have to get the first paramenter. For example:
                      i have this: /home/user/image.jpeg

                      For this, you're better of using Qt's file/dir/fileinfo classes. For the simplest of examples:

                      QString filePath = "/home/user/image.jpeg";
                      QString dirPath = QFileInfo(filePath).dir().path();
                      

                      @SGaist said:

                      You'll be also interested by the regular expression tool

                      Thanks a lot. I have done good what I wanted to do :)
                      Have a good day.

                      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
                      • Unsolved