I've a string and I'd like to parse the one on structure blocks.
So, structure in string like this:
if(true) {
if(true) {
if(true) {}
}
}
if(true) {
if(true) {
if(true) {}
}
}
And I'd like to split the one on parent blocks like this:
if(true) {
if(true) {
if(true) {}
}
},
if(true) {
if(true) {
if(true) {}
}
}
My code:
string condition =
"if(true) {\
if(true) {\
if(true) {}\
}\
}\
if(true) {\
if(true) {\
if(true) {}\
}\
}";
string item;
stringstream stream(condition);
vector<string> array;
//splitting on sections
while (getline(stream, item, '}')) {
array.push_back(item + "}");
}
for(int i = 0; i < array.size(); i++) {
cout << i << array[i] << endl;
}
Result:
0 if(true) { if(true) { if(true) {}
1 }
2 }
3 if(true) { if(true) { if(true) {}
4 }
5 }
But need:
0 if(true) { if(true) { if(true) {} } }
1 if(true) { if(true) { if(true) {} } }
How to detect and parse the parent blocks more correctly or tell an algorithm?