Removing ANSI Color Codes With Regex

Strangely enough, I haven’t really used regular expressions until recently. They’re incredibly powerful. In fact, here’s one that replaces about 40 lines of C++ code with a single line of C#.NET code:

text = Regex.Replace(text, @”\e\[\d*;?\d+m”, “”);

It’s not absolutely perfect, but it does the trick for removing all of the ANSI color codes from a block of text.