Reputation: 23374
I have a situation where commas are stripped when they shouldn't be, e.g.,
John 3:16,18,4:2 becomes John 3:16 18 4:2
I would like to put the commas back, and thought I could do it with
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);
but this just gives me
John 3:16,18 4:2
What am I missing?
Upvotes: 1
Views: 187
Reputation: 2930
You can try something with lookbehind:
@"(?<=\d)\s+(\d+)"
and replace with
,$1
Upvotes: 1
Reputation: 208465
Use a lookbehind and lookahead so the digits are not a part of your match:
string strRegex = @"(?<=\d)\s+(?=\d)";
...
string strReplace = @",";
Upvotes: 5