简体   繁体   中英

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

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.

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])

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.

尝试用空字符串替换。

Regex.Replace(input, @"(?![0-9])\s*,\s*(?![0-9])", "");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM