I don't think there's a function in Qt that does it for you, but you could easily reconstruct it. Pseudo code, because I don't know the exact syntax:
QStringList query = text.split(rx);
QStringList queryWithSeparators;
size_t pos = 0;
for (const auto part : query) {
queryWithSeparators.append(part);
pos += part.length;
if (pos + 1 < text.length) {
// we know that the separators are all 1 character long
queryWithSeparators.append(text.substring(pos, 1));
pos += 1;
}
}
This is ugly and difficult to understand. From you example it seems that you are trying to parse a mathematical expression. It's much easier to create a tokenizer which reads character-by-character than trying to use regular expressions for this task.
(If you really want to use split
, you could first split it for all +
, then split these strings at all -
etc. This way you would know exactly what the separators are.)