用空格和逗号用分号替换返回?

Pad*_*wan 1 perl

我希望能够在单个字符串(不是整个文件,只是程序中的一个字符串)中替换所有行返回(\n),空格和所有逗号在同一个字符串中用分号.

这是我的代码:

    $str =~ s/"\n"/" "/g;
    $str =~ s/","/";"/g;
Run Code Online (Sandbox Code Playgroud)

hwn*_*wnd 9

这样做.您不需要在它们周围使用引用.

$str =~ s/\n/ /g;
$str =~ s/,/;/g;
Run Code Online (Sandbox Code Playgroud)

替换运算符的修饰符选项说明(s///)

e       Forces Perl to evaluate the replacement pattern as an expression. 
g       Replaces all occurrences of the pattern in the string. 
i       Ignores the case of characters in the string. 
m       Treats the string as multiple lines. 
o       Compiles the pattern only once. 
s       Treats the string as a single line. 
x       Lets you use extended regular expressions. 
Run Code Online (Sandbox Code Playgroud)