如何在c#中的字符串中间添加新行

时间:2012-05-05 08:51:43

标签: regex c#-4.0

我有以下字符串,其中包含对产品的评论,我想将13个中的1个和13个中的4个句子移动到新行中

输入字符串

The mapping features have a lot of inaccuracie 1 of 3
am a little disappointed in the new 4S. 4 of 13

输出字符串

The mapping features have a lot of inaccuracies 
1 of 3
am a little disappointed in the new 4S 
4 of 13

我正在尝试Regex.Replace,因为它会更改字符串

中的所有匹配项

我使用@"\d+ of \d+"

找到了字符串

但是如何在变更文本中保留变量号?或者你能提出一个不同的方法吗?

1 个答案:

答案 0 :(得分:2)

您需要捕获匹配项,以便能够将其用于替换:

@"(\d+ of \d+)"

要替换此值,请使用$1

类似的东西:

Regex.Replace(input, 
              @"(\d+ of \d+)", 
              string.Format(@"{0}$1{0}", Environment.NewLine))