求解一个正表达式替换问题
原来方法:
public void Replace(string profile, string newstr, bool bIgnoreCase)
{
string pattern = Regex.Escape(profile);
string instead = newstr.Replace("$", "$$");
pattern = pattern.Replace(@"\[变量]", @"[\s\S]*?");
string[] _pattern = pattern.Split(new char[] { '[', '过', '滤', '字', '符', '串', ']' }, StringSplitOptions.RemoveEmptyEntries);
string p = "";
string n = "";
for (int i = 0; i < _pattern.Length; i++)
{
string s = _pattern[i];
if (!s.Equals(""))
{
p += "(?<ch" + i + ">" + s +@")[\s\S]+?";
n += "${ch" + i + "}" + instead;
}
}
Regex reg;
if (bIgnoreCase)
reg = new Regex(p, RegexOptions.Compiled | RegexOptions.IgnoreCase);
else
reg = new Regex(p, RegexOptions.Compiled);
m_content = reg.Replace(m_content, n);
}
说明:profile为一个正则表达式或替换前的字符串, newstr为替换后的字符串,m_content为待替换的字符串(如一篇文章)。
怎么样修改上面方法来实现批量替换,其效果为: 要把“中华人民共和国”替换为“中国”。(上面方法能执行,但不是其效果。)
注:请不要给出:m_content.Replace(oldstr, newstr); 等类似的方法。