MatchEvaluator enables you to perform custom verification and manipulation for each single match found by the Regex.Replace method, probably I'm just too pedantic about it, okay, here comes the code:
using System;
using System.Text.RegularExpressions;
namespace RegexExerciseI7
{
class Program
{
static void Main(String[] args)
{
Regex regex = new Regex(@"<(?<slash>/?)(?<tag>[^>]+)>", RegexOptions.IgnoreCase);
Console.WriteLine("Type in the HTML text you want to process:");
String inputHtml = Console.ReadLine();
String resultHtml = regex.Replace(inputHtml, delegate(Match match)
{
String slash = match.Groups["slash"].Value;
String tag = match.Groups["tag"].Value.ToUpperInvariant();
return String.Equals(slash, "/") ? String.Format("</{0}>", tag) : String.Format("<{0}>", tag);
});
Console.WriteLine(resultHtml);
}
}
}
In this code snippet, you will find that I perform an extra check to make sure that all the starting tags(e.g <html>) and ending tags(e.g </html>) will be considered.using System.Text.RegularExpressions;
namespace RegexExerciseI7
{
class Program
{
static void Main(String[] args)
{
Regex regex = new Regex(@"<(?<slash>/?)(?<tag>[^>]+)>", RegexOptions.IgnoreCase);
Console.WriteLine("Type in the HTML text you want to process:");
String inputHtml = Console.ReadLine();
String resultHtml = regex.Replace(inputHtml, delegate(Match match)
{
String slash = match.Groups["slash"].Value;
String tag = match.Groups["tag"].Value.ToUpperInvariant();
return String.Equals(slash, "/") ? String.Format("</{0}>", tag) : String.Format("<{0}>", tag);
});
Console.WriteLine(resultHtml);
}
}
}