正则双重过滤 /// splitKey1 第一个正则式匹配 /// splitKey2 匹配结果中再次匹配进行替

时间:2022-03-21 03:22:32
        /// <summary>
/// 正则双重过滤
/// splitKey1 第一个正则式匹配
/// splitKey2 匹配结果中再次匹配进行替换
/// </summary>
/// <param name="content"></param>
/// <param name="splitKey1"></param>
/// <param name="splitKey2"></param>
/// <param name="newChars"></param>
/// <returns></returns>
private static string GetReplace(string content, string splitKey1, string splitKey2, string newChars)
{
if (splitKey1 != null && splitKey1 != "" && splitKey2 != null && splitKey2 != "")
{
Regex rg = new Regex(splitKey1);
System.Text.RegularExpressions.MatchCollection mc = rg.Matches(content); foreach (System.Text.RegularExpressions.Match mc1 in mc)
{
string oldChar = mc1.ToString();
string newChar = new Regex(splitKey2, RegexOptions.IgnoreCase).Replace(oldChar, newChars);
content = content.Replace(oldChar, newChar);
}
return content;
}
else
{
if (splitKey2 != null && splitKey2 != "")
{
Regex rg = new Regex(splitKey2, RegexOptions.IgnoreCase);
return rg.Replace(content, newChars);
}
}
return content;
}