Replacing a comma with Regex in C#
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
No comments:
Post a Comment