fork(1) download
  1. using System;
  2. using System.Linq;
  3. using System.Text.RegularExpressions;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var s = "<Element><Element value=\"'hello&stack<overflow>'\" value=\"'hi&stack<over flow2 >'\"/></Element>";
  10. var rx = @"((?:<[a-zA-Z][\w:-]*|\G(?!\A))\s+[^\s=<]*=)(""[^""]*"")";
  11. var clean = Regex.Replace(s, rx, m =>
  12. string.Format("{0}{1}", m.Groups[1].Value, m.Groups[2].Value.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;"))
  13. );
  14. Console.Write(clean);
  15. }
  16. }
Success #stdin #stdout 0.04s 30136KB
stdin
Standard input is empty
stdout
<Element><Element value="'hello&amp;stack&lt;overflow&gt;'" value="'hi&amp;stack&lt;over flow2 &gt;'"/></Element>