c#Regex替换\ n

时间:2011-04-23 15:36:40

标签: c# regex

我正在尝试删除[/quote]

之后的所有换行符

我目前有这个:

Comment = Regex.Replace(Comment, @"[/quote](\n){1,}", "[/quote]");

但它似乎没有做任何事情!

示例:

[/quote]


hey nice quote blah blah

转到

[/quote]hey nice quote blah blah

4 个答案:

答案 0 :(得分:3)

你确定你的字符串以\n结尾(UNIX风格的行结尾),而不是\r\n(Windows风格的行结尾)?

另外,请注意正则表达式中的[...]表示字符类,因此[/quote]匹配单个字符/q,{{1} },uot。您必须将e转义为[以匹配开括号字符。

将它们放在一起(并将\[简化为速记{1,}),然后尝试:

+

答案 1 :(得分:0)

在“\ n”之后添加“+”以匹配所有\ n的

答案 2 :(得分:0)

你还需要逃脱换行符 [/报价] [\\ N] +

答案 3 :(得分:0)

尝试使用此正则表达式

string strRegex = @"\[/quote\][\n\r]+";   
Regex myRegex = new Regex(strRegex);

string strReplace = "[/quote]";
return myRegex.Replace(strTargetString, strReplace);