Perl 用空格替换返回,用分号替换逗号?
我希望能够将单个字符串中的所有行返回(\n)替换为空格,将同一字符串中的所有逗号替换为分号 这是我的密码:Perl 用空格替换返回,用分号替换逗号?,perl,Perl,我希望能够将单个字符串中的所有行返回(\n)替换为空格,将同一字符串中的所有逗号替换为分号 这是我的密码: $str =~ s/"\n"/" "/g; $str =~ s/","/";"/g; 您不需要在搜索和替换中引用,只需要在第一个示例中表示一个空格(或者您也可以执行/) 这就行了。你不需要在它们周围使用引号 $str =~ s/\n/ /g; $str =~ s/,/;/g; 替换运算符(s//)的修饰符选项说明 我会使用: 实际上,在perl中,/在这里被视为引用字符
$str =~ s/"\n"/" "/g;
$str =~ s/","/";"/g;
您不需要在搜索和替换中引用,只需要在第一个示例中表示一个空格(或者您也可以执行
/
)
这就行了。你不需要在它们周围使用引号
$str =~ s/\n/ /g;
$str =~ s/,/;/g;
替换运算符(s//
)的修饰符选项说明
我会使用:
实际上,在perl中,
/
在这里被视为引用字符(分隔符)。有详细资料。。。分隔符(您选择的,它不必是/
)引用regexp。您可能需要使用s
修饰符才能匹配换行符。@BenVoigt:s
修饰符只更改qr/
的含义。显式\n
在多行输入上不使用时,可以按预期工作。
$str =~ s/\n/ /g;
$str =~ s/,/;/g;
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.
$str =~ tr/\n,/ ;/;