我有一种情况,逗号在不应该被删除的时候被删除,例如,
John 3:16,18,4:2 becomes John 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);
但这只是给了我
John 3:16,18 4:2
我错过了什么?
请您参考如下方法:
使用 lookbehind and lookahead所以数字不是你匹配的一部分:
string strRegex = @"(?<=\d)\s+(?=\d)";
...
string strReplace = @",";
