2012-05-05 121 views
0

我有一个包含对产品的评论下面的字符串,我要像3 1 13 4句搬进新线如何在C#中添加一个新行字符串中间

输入字符串

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,因为它改变了串

0123中所有出现

我找到字符串使用@"\d+ of \d+"

但我怎样才能保持替换文本中的变量号码?或者你能否提出一种不同的方法?

回答

2

您需要捕获匹配以能够用它来替换:

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

要更换此值,使用$1

是这样的:

​​
+0

该命令删除了原始文本(1 3),并通过\ 1代替它。我想保留这些号码,只是在他们之前添加新行 –

+0

@ahmadjawdat - 对不起 - 我得到反向引用语法错误。第一个捕获的是parens而不是'1',它的价格是$ 1。答案已更新。 – Oded

+0

谢谢,它现在有效 –

相关问题