I have a string coming from a telnet client. This string contains backspace characters which I need to apply. Each backspace should remove one previously typed character.
This is basically a variant of How can we match a^n b^n with Java regex?, so we could reuse its answer there:
var regex = new Regex(@"(?:[^\b](?=[^\b]*((?>\1?)[\b])))+\1");
Console.WriteLine(regex.Replace("Hello7\b World123\b\b\b", ""));
Additionally, the .NET regex engine supports balancing groups, so we could use a different pattern:
var regex = new Regex(@"(?[^\b])+(?[\b])+(?(L)(?!))");
(This means:
(?!) matches nothing).)