求!请问这个正则表达式该如何写?
有一个字符串
string StringInsert= "Hi:\r\nThis is James!\nNice to meet you!\n\nThanks. "
我想把所有 '\n '替换成‘\r\n’,但是如果\n前面已经有了\r的话,就不必重复替换了。
也就成最终我想要的字符串应该是:
"Hi:\r\nThis is James!\r\nNice to meet you!\r\n\r\nThanks. "
我用了一下代码,好像不对!
string rstr = "[\r]{0}[\n]{1} ";
Regex r = new Regex(rstr, RegexOptions.IgnoreCase | RegexOptions.Compiled);
if (r.IsMatch(StringInsert))
StringInsert=r.Replace(StringInsert, "\r\n ");
请问,正确的匹配条件应该如何写?
谢谢!
------解决方案--------------------string StringInsert = "Hi:\r\nThis is James!\nNice to meet you!\n\nThanks. ";
string rstr = "(? <!\\r)\\n ";
Regex r = new Regex(rstr, RegexOptions.IgnoreCase | RegexOptions.Compiled);
if (r.IsMatch(StringInsert))
{
StringInsert = r.Replace(StringInsert, "\r\n ");
}