我正在尝试删除 [/quote]
后的任何换行符。
目前我的代码如下:
Comment = Regex.Replace(Comment, @"[/quote](\n){1,}", "[/quote]");
但它似乎没有起到任何作用!
示例:
[/quote]
hey nice quote blah blah
前往
[/quote]hey nice quote blah blah
\n
结尾,而不是Windows风格的换行符\r\n
吗?[...]
表示字符类,因此你的[/quote]
匹配一个字符,它可以是/
、q
、u
、o
、t
或e
。你必须将[
转义为\[
来匹配一个开括号字符。{1,}
简化为简写形式+
),然后尝试这个:Regex.Replace(Comment, @"\[/quote\][\r\n]+", "[/quote]");
你还需要转义换行符 [/quote][\\n]+
尝试使用这个正则表达式
string strRegex = @"\[/quote\][\n\r]+";
Regex myRegex = new Regex(strRegex);
string strReplace = "[/quote]";
return myRegex.Replace(strTargetString, strReplace);
[\r\n]
匹配其中任何一个吗? - Tom Gullen\r
、\n
、\r\n
、\n\r
、\r\r\r\r
、\n\n\n\n\n\n\n\n\n\n\n\n\n\n
、\r\r\n\r\n\n\n\r
等。 - Callum Rogers