Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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
C# 重叠正则表达式替换_C#_Regex - Fatal编程技术网

C# 重叠正则表达式替换

C# 重叠正则表达式替换,c#,regex,C#,Regex,我遇到过一种情况,逗号在不应该的时候被去掉,例如 约翰福音3:16,18,4:2变成了约翰福音3:16,18,4:2 我想把逗号放回去,我想我可以用 string strRegex = @"(\d+)\s+(\d+)"; RegexOptions myRegexOptions = RegexOptions.None; Regex myRegex = new Regex(strRegex, myRegexOptions); string strTargetString = @"John 3:16

我遇到过一种情况,逗号在不应该的时候被去掉,例如

约翰福音3:16,18,4:2变成了约翰福音3:16,18,4:2

我想把逗号放回去,我想我可以用

string strRegex = @"(\d+)\s+(\d+)";
RegexOptions myRegexOptions = RegexOptions.None;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = @"John 3:16 17 4:3";
string strReplace = @"$1,$2";

return myRegex.Replace(strTargetString, strReplace);
但这只会让我

约翰福音3:16,18:4:2

我遗漏了什么?

使用a,这样数字就不会成为匹配的一部分:

string strRegex = @"(?<=\d)\s+(?=\d)";
...
string strReplace = @",";
string stregex=@”(?使用a,这样数字就不会成为匹配的一部分:

string strRegex = @"(?<=\d)\s+(?=\d)";
...
string strReplace = @",";

string stregex=@”(?您可以尝试使用lookback:

@"(?<=\d)\s+(\d+)"

您可以尝试使用Lookback:

@"(?<=\d)\s+(\d+)"