PDA

View Full Version : Separate the words by QRegex



stereoMatching
25th July 2012, 09:36
QString contents = "lll-###";

QRegExp split("\\b");

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


What I want is "lll-", "###"
The result is "lll", "-###"

What should I do?Thanks

amleto
25th July 2012, 10:48
1) work around it.
2) use a different method (string.split ?)

stereoMatching
25th July 2012, 12:23
1) work around it.
My pretty nasty work around



//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;
}


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

ChrisW67
26th July 2012, 07:38
Two other ways:


QString contents = "lll-###";

QStringList result;
int index = contents.lastIndexOf('-');
result << contents.left(index + 1) << contents.mid(index + 1);
qDebug() << result;


result.clear();
QRegExp re("(.+-)(#+)");
if (re.indexIn(contents) >= 0)
result << re.cap(1) << re.cap(2);
qDebug() << result;

This is where you tell us that this was not really the requirement you had in mind.