开发者

regex/linq to replace consecutive characters with count

I have the following method (written in C#/.NET). Input text consist only of letters (no开发者_JAVA技巧 digits). Returned value is another text in which groups of more than two consecutive characters are replaced with one the character preceded with a count of repetitions. Ex.: aAAbbbcccc -> aAA3b4c

public static string Pack(string text)
{
    if (string.IsNullOrEmpty(text)) return text;

    StringBuilder sb = new StringBuilder(text.Length);

    char prevChar = text[0];
    int prevCharCount = 1;

    for (int i = 1; i < text.Length; i++)
    {
        char c = text[i];
        if (c == prevChar) prevCharCount++;
        else
        {
            if (prevCharCount > 2) sb.Append(prevCharCount);
            else if (prevCharCount == 2) sb.Append(prevChar);
            sb.Append(prevChar);

            prevChar = c;
            prevCharCount = 1;
        }
    }

    if (prevCharCount > 2) sb.Append(prevCharCount);
    else if (prevCharCount == 2) sb.Append(prevChar);
    sb.Append(prevChar);

    return sb.ToString();
}

The method is not too long. But does any one has an idea how to do that in a more concise way using regex? Or LINQ?


How about:

static readonly Regex re = new Regex(@"(\w)(\1){2,}", RegexOptions.Compiled);
static void Main() {
    string result = re.Replace("aAAbbbcccc",
         match => match.Length.ToString() +  match.Value[0]);   
}

The regex is a word char, followed by the same (back-ref) at least twice; the lamba takes the length of the match (match.Length) and appends the first character (match.Value[0])

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜