Regular expression to remove a specific repeated character

I would like to create a regular expression in C # that deletes a specific character if it repeats and therefore is not the last character of the string.

Example:

"a--bc-" => "abc" "-ab--c" => "abc" "--a--b--c--" => "abc" 

I never want to repeat, and I never want it to be the first or last character of my string. How could I write a regex for this?

+6
c # regex
source share
5 answers

Probably the easiest way to do this in two steps. First, replace each occurrence of one or more "-" with one "-", then trim any leading / trailing "-".

 var reducedString = Regex.Replace(inputString, "-+", "-"); var finalString = reducedString.Trim('-'); 
+14
source share

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"); } } } 
+4
source share
 string tidyText = Regex.Replace(originalText, "^-+|(?<=-)-+|-+$", ""); 
+3
source share

I know that you asked for Regex, but the second one you need to change or re-read is the code that most people simply rewrite, because it is faster than retraining what the code does. 2 lines using the built-in line methods will be much easier than re-reading the future regex. And in some cases it’s faster.

  string text = "--ab--c-"; text = text.Replace( "--", "-" ); text = text.Trim( '-' ); 
+1
source share

This might be easier to do without regular expressions. Something like the following (untested):

 string s = "--a--b--c--"; string t = ""; bool atStart = true; bool inHyphen = false; foreach (char c in s) { if (c != "-") { if (atStart) { atStart = false; } else if (inHyphen) { inHyphen = false; t += "-"; } t += c; } else { inHyphen = true; } } 
0
source share

All Articles