Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to apply backspace characters

Tags:

c#

regex

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.

I'm trying to do this in a single replace using regular expression:

string txt = "Hello7\b World123\b\b\b";
txt = Regex.Replace(txt, ".\\\b", "", RegexOptions.ECMAScript);

Which results in "Hello World12". Of course, I want "12" to be removed too, but it obviously doesn't match my expression.

In some way, it should repeat replacing until there are no more matches. Any ideas on how to achieve this with a single regular expression?

like image 851
huysentruitw Avatar asked May 17 '13 08:05

huysentruitw


People also ask

What is the regular expression for characters?

A regex (regular expression) consists of a sequence of sub-expressions. In this example, [0-9] and + . The [...] , known as character class (or bracket list), encloses a list of characters. It matches any SINGLE character in the list.

How do you replace characters with backspace in Java?

Java regex - erase characters followed by \b (backspace)

What does \\ mean in regex?

\\. matches the literal character . . the first backslash is interpreted as an escape character by the Emacs string reader, which combined with the second backslash, inserts a literal backslash character into the string being read. the regular expression engine receives the string \. html?\ ' .

Is backspace a special character?

A dedicated symbol for "backspace" exists as U+232B ⌫ but its use as a keyboard label is not universal. The backspace is distinct from the delete key, which in paper media for computers would punch out all the holes to strike out a character, and in modern computers deletes text following it.


1 Answers

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(@"(?<L>[^\b])+(?<R-L>[\b])+(?(L)(?!))");

(This means:

  1. Match one or more non-backspaces, assigning them with the name "L",
  2. then followed one or more backspaces, assigning them with the name "R", with the condition that every "R" must have one corresponding "L",
  3. if there are any "L"s left, abandon the match (as (?!) matches nothing).

)

like image 127
kennytm Avatar answered Oct 29 '22 13:10

kennytm