C# 重叠正则表达式替换
我遇到过一种情况,逗号在不应该的时候被去掉,例如 约翰福音3:16,18,4:2变成了约翰福音3:16,18,4:2 我想把逗号放回去,我想我可以用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
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+)"