c# Regexp replace

本文关键字:replace Regexp | 更新日期: 2023-09-27 17:52:13

我试图删除[/quote]后的任何新行字符

我有这个当前:

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

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

的例子:

[/quote]

hey nice quote blah blah

[/quote]hey nice quote blah blah

c# Regexp replace

你确定你的字符串以'n (unix风格的行结束)结束,而不是'r'n (windows风格的行结束)?

另外,要认识到regex中的[...]表示一个字符类,因此您的[/quote]匹配的单个字符是/quote。您必须将[转义为'[以匹配开括号字符。

将它们放在一起(并将{1,}简化为简写的+),并尝试如下:

Regex.Replace(Comment, @"'[/quote']['r'n]+", "[/quote]");

在"'n"后面加一个"+"来匹配所有的'n

还需要转义换行符[/引用][' ' n] +

尝试使用这个正则表达式

string strRegex = @"'[/quote']['n'r]+";   
Regex myRegex = new Regex(strRegex);
string strReplace = "[/quote]";
return myRegex.Replace(strTargetString, strReplace);