Separate QString by QRegex



  • [code]
    QString contents = "lll-###";

    QRegExp split("\b");

    QStringList splitContents = contents.split(split, QString::SkipEmptyParts);
    for(QString const &data : splitContents)
    {
    qDebug() << data;
    }
    [/code]

    The result is "lll", "-###"
    How could I told the QReg if I want the result become
    "lll-", "###"

    I want to make "#+"(I mean "#", "##", "###" and so on) as word boundary

    example
    "aaabbb%### ddd###" will be
    "aaabbb%", "###", " ddd", "###"

    Thanks



  • [QUOTE]1) work around it.[/QUOTE]
    My pretty nasty work around

    [code]
    //helper function
    std::list<std::pair<int, int> > get_positions(QRegExp const &rx, QString const &contents)
    {
    int pos = 0;
    std::list<std::pair<int, int> > result;
    while((pos = rx.indexIn(contents, pos)) != -1)
    {
    //qDebug() << pos2 << ", "<< rx.matchedLength();
    result.push_back(std::make_pair(pos, rx.matchedLength()));
    pos += rx.matchedLength();
    }

    return result;
    }

    //helper function
    template<typename T>
    QStringList const split_str_by_positions(T const &position, QString const &contents)
    {
    QStringList results;
    for(std::pair<int, int> const &data : position)
    {
    results << contents.mid(data.first, data.second);
    }

    return results;
    

    }

    //split the contents into the QStringList
    void test_get_positions()
    {
    QString contents = "lll ### ### ###";

    typedef std::pair<int, int> DInt;
    std::list<DInt> positions = get_positions(QRegExp("#+"), contents);
    
    std::list<DInt> positions2 = get_positions(QRegExp("[^#]+"), contents);
    
    struct DIntCompare
    {
        bool operator()(DInt const &one, DInt const &two) const
        { return one.first < two.first; }
    };
    
    positions.merge(positions2, DIntCompare());
    
    qDebug() << "---------------------";
    
    for(DInt const &data : positions) qDebug() << data.first <<", "<<data.second;
    
    QStringList results = split_str_by_positions(positions, contents);
    
    for(QString const &data : results) qDebug() << data;
    

    }
    [/code]

    Do you have other easier solutions?
    My workaround is ugly, want to find other better way to implement it.
    Thanks



  • you could just call contents.split("###"), but you won't get "###" in your resulting list.
    (Which shouldn't be a big deal, just add them again afterwards if you need them...)
    Try something like that:

    [code]
    QString contents = "lll-###";

    QString split("###");

    QStringList splitContents = contents.split(split, QString::SkipEmptyParts);

    for (int i = splitContents.count(); i >= 0; i--)
    {
    splitContents.insert(i+1, split);
    }

    for(QString const &data : splitContents)
    {
    qDebug() << data;
    }
    [/code]

    if this solution isn't flexible enough for your purposes, you'll probably have to write your own parser...



  • Not really sure what you want to achieve, but maybe use the "\b-" as QRegExp? When I test it, it gives me two strings in the splitContents. "lll" and "###". I lost the "-" ofcourse of the QString.split function.
    The correct Qt way for the for () is more:
    @ QString contents = "lll-###";

     QRegExp split("\\b-");
    
     QStringList splitContents = contents.split(split, QString::SkipEmptyParts);
     foreach (QString const &data , splitContents)
     {
         qDebug() << data;
     }
    

    @



  • bq. Not really sure what you want to achieve

    I want to implement the rename function like the "XnView" did

    example : aaa_###$##$#
    will produce strings like
    "aaa_000$00$0"
    "aaa_001$01$1" and so on
    it would only replace "#" to integer

    The most easiest solution of mine is
    (1)split the string into
    aaa_,###, $,##, $, #

    (2)concatenate those string and replace "#" to number


Log in to reply
 

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