c# 正则表达式 替换重复文字

类似【第1章 第 1 章】只保留一个【第1章】

//这个写法可以调试
content = Regex.Replace(content, "\n\n第\\d章(?<chapter>.*第.*\\d.*章)\n\n", (o) => {
    return o.Value.Replace(o.Groups["chapter"].Value, "");
});
content = Regex.Replace(content, "\n\n第\\d章(?<chapter>.*第.*\\d.*章)\n\n", o => o.Value.Replace(o.Groups["chapter"].Value, ""));

 

 一开始用了这个,替换后的字符串颜色不是正则表达式的,替换出来以后【第\\d章】,第三个参数也要换成正则表达式的

查看定义还有这几个替换方法

public static string Replace(string input, string pattern, MatchEvaluator evaluator); 找了这个方法可以,用法:https://blog.csdn.net/m0_63120843/article/details/130843529

字符串去重的,https://blog.csdn.net/cathyhaibara/article/details/45097601,有str_leader = Regex.Replace(str_leader + ",", @"(?:([^,]+,))(?=.?\1)", "");没看明白,https://www.cnblogs.com/kgtone/p/11832492.html,和这个网页的 ^(.*?)$\s+?^(?=.*^\1$) 有点类似

 

posted @ 2023-07-26 14:34  户的博客  阅读(173)  评论(0编辑  收藏  举报