This can be solved much easier without requiring a regular expression: You just want to “invert” the area of the string delimited by the first and last occurrence of a “1”.
Here’s an example solution:
string input = "..........1............1...";
int start = input.IndexOf('1');
int end = input.LastIndexOf('1');
char[] content = input.ToCharArray();
for (int i = start; i <= end; i++)
{
content[i] = content[i] == '1' ? '.' : '1'; //invert
}
string output = new string(content);
5
solved Characters between two exact characters [closed]