Split words in letters



  • 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.)


  • Qt Champions 2016

    Hi
    You can use the split function

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


  • Qt Champions 2016

    @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.



  • @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);
    }
    

  • Lifetime Qt Champion

    Hi,

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



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



  • @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!



  • @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!



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


  • Qt Champions 2016

    @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();
    

  • Lifetime Qt Champion

    You'll be also interested by the regular expression tool



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.