简体   繁体   English

QString拆分返回空字符串列表

[英]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 我正在尝试使用此正则表达式B([^.]*)E从中获取BE之间的所有字符

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

Using Qt4.8 使用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 ? 它不应该返回BE之间的所有字符串吗?

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). 主要问题是您试图拆分,但实际上您需要在循环中查找所有匹配项并获取capturedTexts()[1] s(或cap(1) )。

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;

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM