c# 通过正则对多个匹配结果进行个性化替换

void Main()
{
    string input = "abcdef ghi jklmnop qrst uvwxyz";

    // 正则表达式匹配所有字母(包括大小写字母)
    string pattern = @"[a-zA-Z]+";

    // 使用 MatchEvaluator 在替换时处理每个匹配项
    string result = Regex.Replace(input, pattern, match =>
    {
            // 获取前三个字符,防止溢出
            return match.Value.Substring(0, Math.Min(3, match.Length));
    });

    Console.WriteLine("原始字符串: " + input);
    Console.WriteLine("替换后的字符串: " + result);
}

 

posted on 2024-12-21 16:47  空明流光  阅读(85)  评论(0)    收藏  举报

导航