正则表达式 – 用制表符替换多个空格

我正在使用下面的代码示例,我想要做的是用1 \ t替换所有多个空格。

所以

"I am having fun working on Regex" 

回报

 "I am\thaving fun\tworking\ton\tRegex" 

当前代码:

 RegexOptions options = RegexOptions.None; Regex regex = new Regex(@"[ ]{2,}", options); tempo = regex.Replace(tempo, @" "); 

谢谢!

这样做:

 resultString = Regex.Replace(subjectString, " {2,}", @"\t"); 

你忘了你的尝试。

在您的示例中,您将使用单个行替换多个空格:

 tempo = regex.Replace(tempo, @" "); 

你想要的是,用标签字符替换,它是写的\t

 tempo = regex.Replace(tempo, "\t"); 

请注意,我在对Replace的调用中从字符串中删除了@字符,否则@"\t"被解释为“字符串反斜杠t”而不是制表符。