In this exercise, Eric asks us to replace all html tag names with uppercase equivalent. My answer is based on Maurits' suggestion, that is to say using MatchEvaluator.
    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.

相关文章:

  • 2021-11-24
  • 2022-12-23
  • 2021-11-30
  • 2021-12-17
  • 2021-07-15
  • 2022-12-23
猜你喜欢
  • 2021-05-23
  • 2022-12-23
  • 2022-12-23
  • 2021-08-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案