Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何在C中添加一个新的字符串_Regex_C# 4.0 - Fatal编程技术网

Regex 如何在C中添加一个新的字符串

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

我有下面的字符串,其中包含对产品的评论,我想把像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 
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
。答案已更新。