我想以后[/quote]
C#正则表达式替换 n
,以消除任何新行字符我有这个目前:
Comment = Regex.Replace(Comment, @"[/quote](\n){1,}", "[/quote]");
但它似乎并没有做任何事情!
例子:
[/quote]
hey nice quote blah blah
转到
[/quote]hey nice quote blah blah
我想以后[/quote]
C#正则表达式替换 n
,以消除任何新行字符我有这个目前:
Comment = Regex.Replace(Comment, @"[/quote](\n){1,}", "[/quote]");
但它似乎并没有做任何事情!
例子:
[/quote]
hey nice quote blah blah
转到
[/quote]hey nice quote blah blah
你确定你的字符串以\n
(UNIX-sty le行尾),而不是\r\n
(Windows风格的行结尾)?
此外,实现[...]
在正则表达式表示一个字符类,所以你[/quote]
单个字符要么是/
,q
,u
,o
,t
,或者e
匹配。您必须将[
设为\[
以匹配开括号字符。
把它们放在一起(和简化{1,}
的简写+
),并尝试这个办法:
Regex.Replace(Comment, @"\[/quote\][\r\n]+", "[/quote]");
添加一个 “+” 后 “\ n” 匹配所有\ n的
似乎不工作 – 2011-04-23 15:41:14
还需要躲避换行符 [/ quote] [\\ n] +
这不似乎工作etiehr – 2011-04-23 15:45:36
尝试使用这个表达式
string strRegex = @"\[/quote\][\n\r]+";
Regex myRegex = new Regex(strRegex);
string strReplace = "[/quote]";
return myRegex.Replace(strTargetString, strReplace);
非常感谢你!那么'[\ r \ n]'是否匹配他们中的任何一个呢? – 2011-04-23 15:47:55
@Tom:它至少匹配其中的一个,因此所有这些匹配:'\ r','\ n','\ r \ n','\ n \ r','\ r \ r \ r \ r \,\ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ r \ n \ n \ n \ r'等 – 2011-04-23 15:55:47