为什么我仍然收到换行符?
Why am I still getting newlines?
我有以下代码:
open INPUT, "input.txt";
my $line = "";
while (<INPUT>)
{
$line = $_;
$line =~ s/\s+^//;
print $line;
}
但输出仍然包含所有换行符。我也试过 \v
和 \R
.
/\s+^/
表示 "one or more whitespace characters before the start of the string" — 因此它永远不会匹配。
如果您的目标是删除 尾随 空白字符,那么您需要 $
而不是 ^
:
$line =~ s/\s+$//;
(如果您的目标真的只是删除结尾的换行符,那么您可能应该使用 the built-in chomp
function。)
我有以下代码:
open INPUT, "input.txt";
my $line = "";
while (<INPUT>)
{
$line = $_;
$line =~ s/\s+^//;
print $line;
}
但输出仍然包含所有换行符。我也试过 \v
和 \R
.
/\s+^/
表示 "one or more whitespace characters before the start of the string" — 因此它永远不会匹配。
如果您的目标是删除 尾随 空白字符,那么您需要 $
而不是 ^
:
$line =~ s/\s+$//;
(如果您的目标真的只是删除结尾的换行符,那么您可能应该使用 the built-in chomp
function。)