Completely remove duplicate characters (regex / C #)

Looking for one C # string code that removes duplicate characters from a string. We did this with a simple lead ahead of the loop, but would like to see a regular expression. Ex. input = "6200032111623451123345666" output = "623262345245"

Thank.

Lyle

+3
source share
2 answers

What about:

string s = Regex.Replace("6200032111623451123345666", @"(.)\1+", "");

\1+- this is "one or more" (greedy) backreferences to the first capture group, .(any character).

+5
source

s / (([a-zA-z0-9]) \ 1 +) // g, of course, you need to translate it to C #

-1
source

Source: https://habr.com/ru/post/1727996/


All Articles