fork(2) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6.  
  7. public class Test
  8. {
  9. public static void Main()
  10. {
  11. var s = "abc,def,2,100,xyz!,:))))";
  12. Console.WriteLine(Regex.Replace(s, @"(\d),(\d)", "$1$2")); // Does not handle 1,2,3,4 cases
  13. Console.WriteLine(Regex.Replace(s, @"(\d),(?=\d)", "$1")); // Handles consecutive matches with capturing group+backreference/lookahead
  14. Console.WriteLine(Regex.Replace(s, @"(?<=\d),(?=\d)", "")); // Handles consecutive matches with lookbehind/lookahead
  15. Console.WriteLine(Regex.Replace(s, @",(?<=\d,)(?=\d)", "")); // Also handles all cases, the most efficient way
  16. Console.WriteLine(Regex.Replace(s, @"\d,\d", m => m.Value.Replace(",",string.Empty))); // Callback method
  17. }
  18. }
Success #stdin #stdout 0.07s 19692KB
stdin
Standard input is empty
stdout
abc,def,2100,xyz!,:))))
abc,def,2100,xyz!,:))))
abc,def,2100,xyz!,:))))
abc,def,2100,xyz!,:))))
abc,def,2100,xyz!,:))))