正则表达式用逗号替换所有换行符

问题描述:

使用正则表达式,如何用逗号 (,) 替换每个换行符 (\n)?

With regex, how do I replace each newline char (\n) with a comma (,)?

像这样:

Demetrius Navarro
Tony Plana
Samuel L. Jackson

致:

Demetrius Navarro,Tony Plana,Samuel L. Jackson

不是在特定的编程语言中,只是标准的正则表达式.像这样:

Not in a particular programming lang, just standard regex. Something like this:

(.*)
$1
//This just takes the whole string and outputs it as is, I think

要匹配所有换行符,/\n/g.为了替换它们,您需要指定一种语言.例如,在 JavaScript 中:

To match all newline characters, /\n/g. In order to replace them, you need to specify a language. For example, in JavaScript:

str.replace(/\n/g, ",");

实例

一个简单的谷歌搜索揭示了它是如何在 C# 中完成的:

A simple Google search reveals how it's done in C#:

Regex.Replace(str, "\n", ",");

在阅读了您的一些评论后,我搜索了如何在 Perl 中进行操作.应该这样做:

After reading some of your comments, I searched how to do it in Perl. This should do it:

s/\n/,/g;