<p>I'm writing a Telnet client of sorts in C# and part of what I have to parse are ANSI/VT100 escape sequences, specifically, just those used for colour and formatting (detailed <a href="http://www.termsys.demon.co.uk/vtansi.htm#colors" rel="noreferrer">here</a>).</p>

<p>One method I have is one to find all the codes and remove them, so I can render the text without any formatting if needed:</p>

<pre><code>    
public static string StripStringFormating(string formattedString)
{
    if (rTest.IsMatch(formattedString))
        return rTest.Replace(formattedString, string.Empty);
    else
        return formattedString;
}
</code></pre>

<p>I'm new to regular expressions and I was suggested to use this:</p>

<pre><code>static Regex rText = new Regex(@"\e\[[\d;]+m", RegexOptions.Compiled);</code></pre>

<p>However, this failed if the escape code was incomplete due to an error on the server. So then this was suggested, but my friend warned it might be slower (this one also matches another condition (z) that I might come across later):</p>

<pre><code>static Regex rTest = 
              new Regex(@"(\e(\[([\d;]*[mz]?))?)?", RegexOptions.Compiled);</code></pre>

<p>This not only worked, but was in fact faster to and reduced the impact on my text rendering. Can someone explain to a regexp newbie, why? :)</p>

[{'answer_id': 4883, 'author': 'Ryan Fox', 'author_id': 55, 'author_profile': 'https://Stackoverflow.com/users/55', 'pm_score': 1, 'selected': False, 'text': '<p>Without doing detailed analysis, I\'d guess that it\'s faster because of the question marks. These allow the regular expression to be "lazy," and stop as soon as they have enough to match, rather than checking if the rest of the input matches.</p>\n\n<p>I\'m not entirely happy with this answer though, because this mostly applies to question marks after * or +. If I were more familiar with the input, it might make more sense to me.</p>\n\n<p>(Also, for the code formatting, you can select all of your code and press <kbd>Ctrl</kbd>+<kbd>K</kbd> to have it add the four spaces required.)</p>\n'}, {'answer_id': 4915, 'author': 'Jonathan Works', 'author_id': 309844, 'author_profile': 'https://Stackoverflow.com/users/309844', 'pm_score': 2, 'selected': False, 'text': '<p>The reason why #1 is slower is that [\\d;]+ is a greedy quantifier. Using +? or *? is going to do lazy quantifing. See <a href="http://msdn.microsoft.com/en-us/library/3206d374(VS.80).aspx" rel="nofollow noreferrer">MSDN - Quantifiers</a> for more info.</p>\n\n<p>You may want to try:</p>\n\n<pre><code>"(\\e\\[(\\d{1,2};)*?[mz]?)?"\n</code></pre>\n\n<p>That may be faster for you.</p>\n'}, {'answer_id': 52980, 'author': 'Oskar', 'author_id': 5472, 'author_profile': 'https://Stackoverflow.com/users/5472', 'pm_score': 2, 'selected': False, 'text': '<p>Do you really want to do run the regexp twice? Without having checked (bad me) I would have thought that this would work well:</p>\n\n<pre><code>public static string StripStringFormating(string formattedString)\n{    \n    return rTest.Replace(formattedString, string.Empty);\n}\n</code></pre>\n\n<p>If it does, you should see it run ~twice as fast...</p>\n'}, {'answer_id': 84532, 'author': 'Doug Moore', 'author_id': 13179, 'author_profile': 'https://Stackoverflow.com/users/13179', 'pm_score': 1, 'selected': False, 'text': "<p>I'm not sure if this will help with what you are working on, but long ago I wrote a regular expression to parse ANSI graphic files.</p>\n\n<pre><code>(?s)(?:\\e\\[(?:(\\d+);?)*([A-Za-z])(.*?))(?=\\e\\[|\\z)\n</code></pre>\n\n<p>It will return each code and the text associated with it.</p>\n\n<p>Input string:</p>\n\n<pre><code>&lt;ESC&gt;[1;32mThis is bright green.&lt;ESC&gt;[0m This is the default color.\n</code></pre>\n\n<p>Results:</p>\n\n<pre><code>[ [1, 32], m, This is bright green.]\n[0, m, This is the default color.]\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/4870', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/483/']