Advertisement
_CodeBehind

04. Replace a tag

Jun 10th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.98 KB | None | 0 0
  1. using System;
  2. using System.Text.RegularExpressions;
  3.  
  4. public class ReplaceATag
  5. {
  6.     public static void Main()
  7.     {
  8.         var pattern = @"(<a\s)(href=)(""|')(.*)(\3)(>)(.*)(<\/a>)";
  9.         string input;
  10.  
  11.         while ((input = Console.ReadLine()) != "end")
  12.         {
  13.             var match = Regex.Match(input, pattern);
  14.  
  15.             var replaced = Regex.Replace(input, pattern,
  16.                 m => m.Groups[1].Value.Replace(m.Groups[1].Value,"[URL ")
  17.                 + m.Groups[2]
  18.                 + m.Groups[3].Value
  19.                 + m.Groups[4]
  20.                 + m.Groups[5].Value
  21.                 + m.Groups[6].Value.Replace(m.Groups[6].Value,"]")
  22.                 + m.Groups[7]
  23.                 + m.Groups[8].Value.Replace(m.Groups[8].Value, "[/URL]"));
  24.  
  25.             if (match.Success)
  26.             {
  27.                 Console.WriteLine(replaced);
  28.             }
  29.             else
  30.             {
  31.                 Console.WriteLine(input);
  32.             }
  33.         }
  34.  
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement