Так что вопрос довольно простой. После пары часов просмотра тем о регулярных выражениях я все еще не могу найти ту, которая будет обрабатывать строку, как указано в разделе кода.
Вот некоторые из регулярных выражений, которые я пробовал (без экранирования для чтения):
/\d+({.*?})(?:(|\d+|$|))/;
/\d+({.+})(?:(|\d+|$|))/;
/\d+({.*?})(?:(|\d+|\B|))/;
/\d+({.+})(?:(|\d+|\B|))/;
/\d+({.*?})(?:(|\d+|))/;
/\d+({.+})\d+/;
/\d+({.*?})\d+/;
Это самый близкий к тому, что я получил, кроме:
/\d+({.*?})\d+|\d+({.*?})/
QString haystack = "5:4{"type":"someType","data":{"subJson":123}}"\
"9406:22{"type":"SomeOtherType","data":{"subJson":648,"data":{"subSubJson":25}}}"\
"125:10{"last":79}"; // The quotes are obviously escaped but reading sake...
QRegularExpression re = QRegularExpression("\\d+({.*?})\\d+|\\d+({.*?})");
QRegularExpressionMatchIterator i = re.globalMatch(haystack);
QStringList matches;
while (i.hasNext()) {
QRegularExpressionMatch match = i.next();
QString result = match.captured(1); // Group match
matches << result;
}
qDebug() << matches;
Что я ожидаю:
"{"type":"someType","data":{"subJson":123}}"
"{"type":"SomeOtherType","data":{"subJson":648,"data":{"subSubJson":25}}}"
"{"last":79}"
Что я на самом деле получаю:
"{"type":"someType","data":{"subJson":123}}"
"{"type":"SomeOtherType","data":{"subJson":648,"data":{"subSubJson":25}}}"
"" //The last one wasn't matched
НО с полным совпадением я получаю это:
"4{"type":"someType","data":{"subJson":123}}9406"
"22{"type":"SomeOtherType","data":{"subJson":648,"data":{"subSubJson":25}}}125"
"10{"last":79}"