[英]How to replace multiple whitespaces without modifying newline characters?
因此,正如标题所述,我在用单个空格符号替换多个空格和\\ t时遇到了很大的问题。 例如,我有以下字符串:
&!(0-=\\+_)( ^(there goes '\\n') &!(0-=\\+_)(
如您所见,我的字符串同时包含\\ t和\\ s符号。 我已经尝试过这种方法:
line = Regex.Replace(line, @"\t+", " ");
line = Regex.Replace(line, @"\s+", " ");
但是最后我收到的是简单的字符串,没有换行符。 所以....是否有任何方法可以忽略换行符? 提前致谢!
[ ]+
试试这个。它对我有用。请参阅demo。按space
替换。
http://regex101.com/r/lZ5mN8/12
\\s=[\\r\\n\\t\\f ]
它包括newline
。因此它将无法正常工作。
尝试这个:
line = Regex.Replace(line, @"\t+", " ");
line = Regex.Replace(line, @" +", " ");
它将字符串"abc\\t\\t\\t\\nd ef"
为abc \\nd ef
有关说明,请参见例如: http : //www.ntu.edu.sg/home/ehchua/programming/howto/Regexe.html :
The \\s (lowercase s) matches a whitespace (blank, tab \\t, form-feed \\f and newline \\r or \\n).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.