tags:

views:

90

answers:

2

Looking for a one line C# code that would remove repeated chars from a string. Have done it with a simple loop with look-ahead but like to see a regex soln. Ex. input = "6200032111623451123345666" output = "623262345245"

Thanks.

Lyle

A: 

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

ennuikiller
It should be `(([a-zA-z0-9])\2+)`. `\1` is the outermost parenthetic group (which is the whole string in this case).
Amarghosh
+4  A: 

How about:

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

The \1+ is "one or more" (greedy) of the back-reference to the first capture-group, . (any character).

Marc Gravell