fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. public class Test
  7. {
  8. public static void Main()
  9. {
  10. var replacingValues = new Dictionary<string, string> { { "Crazy", "XXX" } };
  11. var test = "viv-ek is a good boy.Mah - esh is Cra - zy.";
  12. var replacedTest =
  13. Regex.Replace(
  14. test,
  15. @"\b(?<part>\w+)(\s*-\s*(?<part>\w+))+\b",
  16. match =>
  17. {
  18. var word = string.Join(string.Empty, match.Groups["part"].Captures.Cast<Capture>().SelectMany(capture => capture.Value));
  19. string replacingValue;
  20. return replacingValues.TryGetValue(word, out replacingValue) ? replacingValue : match.Value;
  21. });
  22. Console.WriteLine(replacedTest);
  23. }
  24. }
Success #stdin #stdout 0.12s 24504KB
stdin
Standard input is empty
stdout
viv-ek is a good boy.Mah - esh is XXX.