gpt4 book ai didi

perl - 用空格替换回车,用分号替换逗号?

转载 作者:行者123 更新时间:2023-12-05 08:17:38 24 4
gpt4 key购买 nike

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

这是我的代码:

    $str =~ s/"\n"/" "/g;
$str =~ s/","/";"/g;

最佳答案

这样就可以了。您不需要在它们周围使用引号。

$str =~ s/\n/ /g;
$str =~ s/,/;/g;

替换运算符的修饰符选项说明(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.

关于perl - 用空格替换回车,用分号替换逗号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18284922/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com