All pastes #338441 Raw Edit

Karlprof

public text v1 · immutable
·published 2007-02-03 12:55 UTC
rendered paste body
using System;
using System.Text.RegularExpressions;

namespace Refs
{
    // Refs shared class.
    class RefsShared
    {
        // Highlight output according to a regular expression.
        public static void Highlight(String Output, Regex Pattern, ConsoleColor Colour)
        {
            // Create an array of "slices" of Output, "cut" by Pattern.
            String[] OutputSlices = Pattern.Replace(Output, "*").Split('*');

            // Loop through all the slices.
            for (int Count = 0; Count < OutputSlices.Length; Count++)
            {
                Console.Write(OutputSlices[Count]);
                MatchCollection Matches = Pattern.Matches(Output);

                // Only between arrays should the match be written.
                if (Count != (OutputSlices.Length - 1))
                {
                    Console.ForegroundColor = Colour;
                    Console.Write(Matches[Count].Value);
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
            }
        }
    }
}