I was thinking about regex as well. However without seeing more examples it is difficult to find the correct pattern. Alternatively you can go over all the string one character after another and find the problematic cases and remove them (or not add them to the StringBuilder).