Reputation: 203
I encountered a problem with quite simple thing I guess, I want to replace each comma ',' in a string except for the ones that are surrounded by digits. Examples:
hey, world -> hey,\nworld
hey , world -> hey,\nworld
they are simple, but now also:
hey,world -> hey,\nworld
hey),world -> hey),\nworld
(1,2) -> (1,2) << no change :P
I tried it with different Regexes and I can't really get it working as easily as I'd like to. Matching the commas that I need is quite easy but the problem is that I thought I can do it this way:
Regex.Replace(input, @"[^\d]\s*,\s*[^\d]", ",\n");
it works cool but it changes my:
hey,world into: he,\norld
I'd be glad if you could help me figure that out :)
Regards, Andrew
Upvotes: 2
Views: 2917
Reputation: 111860
This uses negative lookbehind (?<!...)
and negative lookahead (?!...)
to check for the presence of digits.
(?<![0-9])\s*,\s*|\s*,\s*(?![0-9])
It means: not preceded by digits OR not followed by digits. So the only failure case is: preceded by digits AND followed by digits.
Be aware that \d
is different than [0-9]
. ԱԲԳԴԵԶԷԸԹ0123456789
are \d
(and many others) (they are Armenian numerals), while 0123456789
are [0-9]
My original regex was TOTALLY WRONG! Because it was: not-preceded by digits AND not-followed by digits, while the request was: non-preceded by digits OR not followed by digits.
Upvotes: 8
Reputation: 182
Try to replace with an empty string.
Regex.Replace(input, @"(?![0-9])\s*,\s*(?![0-9])", "");
Upvotes: 0
Reputation: 6122
Your match contains the characters you don't want to replace, you should use the negative lookahead assertion and the negative lookbehind assertion.
Here's a good site for regex.
@"(?<!\d)\s*,\s*(?!\d)"
The above regex will replace the comma and any spaces directly before or after it.
Upvotes: 1
Reputation: 17631
You need to use lookaheads to only match the comma, not the characters before and after the comma:
(?=[^\d]\s*),(?=\s*[^\d])
Adding the removal of spaces shown in the second example:
(?=[^\d]\s*)[ ]*,[ ]*(?=\s*[^\d])
Upvotes: 1