简体   繁体   English

在C#中用Regex替换逗号

[英]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. 这使用负向lookbehind (?<!...)和负向前瞻(?!...)来检查是否存在数字。

(?<![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. 所以唯一的失败案例是:前面有数字AND后跟数字。

Be aware that \\d is different than [0-9] . 请注意\\d[0-9]不同。 ԱԲԳԴԵԶԷԸԹ0123456789 are \\d (and many others) (they are Armenian numerals ), while 0123456789 are [0-9] ԱԲԳԴԵԶԷԸԹ0123456789\\d (和许多其他人)(他们是亚美尼亚数字 ),而0123456789[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])", "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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