Vlad
Vlad

Reputation: 45

QString split return list of empty strings

Using this regex B([^.]*)E I am trying to get all the characters between B and E from

B23432|234|24EB23432|2834|234EB23432|2134|234E

Using Qt4.8

QRegExp rx("B([^.]*)E");
rx.setMinimal(true);
QString str = "B23432|234|24EB23432|2834|234EB23432|2134|234E";
QStringList list;
list = str.split(rx);
qDebug() << list;

it prints a list of empty strings. Shouldn't it return all the strings between B and E?

Upvotes: 1

Views: 1219

Answers (2)

Paul H.
Paul H.

Reputation: 382

This works as well. If there is something wrong with this please let me know.

QRegExp rx("[B(.*)E]");
rx.setMinimal(true);
QString str = "B23432|234|24EB23432|2834|234EB23432|2134|234E";
QStringList list;
list = str.split(rx, QString::SkipEmptyParts);
qDebug() << list;

Upvotes: 1

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626689

The main issue is that you are trying to split, but in fact you need to find all matches in a loop and get capturedTexts()[1]s (or cap(1)s).

QRegExp rx("B([^E]*)E");
rx.setMinimal(true);
QString str = "B23432|234|24EB23432|2834|234EB23432|2134|234E";
QStringList list;
int pos = 0;

while ((pos = rx.indexIn(str, pos)) != -1) {
    list << rx.cap(1);
    pos += rx.matchedLength();
}
qDebug() << list;

Upvotes: 1

Related Questions