For this particular problem, I probably won't use regex. Instead, I would use a combination of String.Split and String.Join , which would be simpler and rather faster:
Like this:
string.Join("-", s.Split(new char[] {'-'}, StringSplitOptions.RemoveEmptyEntries));
With tests:
using System; class Program { static string RemoveDashes(string s) { return string.Join("-", s.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries)); } static void Main(string[] args) { Tuple<string, string>[] tests = new Tuple<string,string> [] { new Tuple<string, string> ("a--bc-", "abc"), new Tuple<string, string> ("-a--bc-", "abc"), new Tuple<string, string> ("--a--b--c--", "abc"), }; foreach (var t in tests) { string s = RemoveDashes(t.Item1); Console.WriteLine("{3}: {0} => Expected: {1}, Actual: {2}", t.Item1, t.Item2, s, s == t.Item2 ? "PASS" : "FAIL"); } } }
Justin grant
source share