How to search a string, backwards, until you find a character and then remove all instances of another character?

  c++, find, string, substring

I am dealing with strings in C++ and I am new to the language and this problem is driving me crazy. Imagine I have an String, something like this:

…moretext;"My error message error n code 12345":ERRORmoretext…

This is one tiny fraction of the string that I’m dealing with, that I’ll use to explain myself. The full strings looks more like:

data;1234;moredata;"My error n message error code 12345":ERROR
There is a ‘n’ between this two lines that belong to the same string wich I don’t want to remove.
data;4567;moredata;"My error message error n code 67890":ERROR

Given the last position of that string, I would want to search it backwards, char by char, until I get to the ";" character. Then, I would have starting and ending positions of my substring, and I could remove all instances of a particular character. In my example I want to remove "n".

My idea was doing something like this:

vector<size_t> position;
string search = "ERROR";
size_t pos = my_str.find(search);
while (pos != std::string::npos)
    pos = my_str.find(search, pos + search.size());

With this code I would have all the positions where ERROR is in my big string. Then I could do:

size_t to;
//for each position of the keyword "ERROR".
for (int i = 0; i < positions.size(); i++) {
    //we search on the string backwards until we find ';'.
    for (int j = my_str.length(); j <[i]) && j >= 0; j--) {
to= my_str.rfind([i], ';');
//As we have starting and ending positions, we could remove all 'n' between that positions, if any.
                my_str.erase(std::remove(my_str.begin(),[i]), 'n'),[i]));

At the end, I would want that substring to become part of my original string, but with the posible ‘n’ between that positions removed, if any.

I hope I’m being clear enough, if not please tell me and I could try to improve my explanation.

Thank you very much.

Source: Windows Questions C++