Regex 如何在C中添加一个新的字符串
我有下面的字符串,其中包含对产品的评论,我想把像3中的1和13中的4这样的句子移到新行中 输入字符串Regex 如何在C中添加一个新的字符串,regex,c#-4.0,Regex,C# 4.0,我有下面的字符串,其中包含对产品的评论,我想把像3中的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
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
输出字符串
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+”
但是如何在替换文本中保留变量号呢?或者您可以建议另一种方法吗?您需要捕获匹配,以便能够使用它进行替换:
@"(\d+ of \d+)"
要替换此值,请使用$1
比如:
Regex.Replace(input,
@"(\d+ of \d+)",
string.Format(@"{0}$1{0}", Environment.NewLine))
此命令删除了原始文本(第1个,共3个),并将其替换为\1。我想保留这些数字,然后在之前添加新行them@ahmadjawdat-抱歉-我把反向引用语法搞错了。第一次捕获参数是
$1
,而不是\1
。答案已更新。