我有一个从文本文件读取的字符串,但在 Ubuntu Linux 中,我尝试从末尾删除其换行符。
我用了所有的方法。但对于s/\n|\r/-/
(我看看它是否找到任何替换任何新行字符串)它替换了字符串,但当我打印它时它仍然转到下一行。此外,当我使用chomp
or chop
,字符串被完全删除。我找不到任何其他解决方案。我该如何解决这个问题?
use strict;
use warnings;
use v5.12;
use utf8;
use encoding "utf-8";
open(MYINPUTFILE, "<:encoding(UTF-8)", "file.txt");
my @strings;
my @fileNames;
my @erroredFileNames;
my $delimiter;
my $extensions;
my $id;
my $surname;
my $name;
while (<MYINPUTFILE>)
{
my ($line) = $_;
my ($line2) = $_;
if ($line !~ /^(((\X|[^\W_ ])+)(.docx)(\n|\r))/g) {
#chop($line2);
$line2 =~ s/^\n+//;
print $line2 . " WRONG FORMAT!\n";
}
else {
#print "INSERTED:".$13."\n";
my($id) = $13;
my($name) = $2;
print $name . "\t" . $id . "\n";
unshift(@fileNames, $line2);
unshift(@strings, $line2 =~ /[^\W_]+/g);
}
}
close(MYINPUTFILE);
删除 Unicode 换行字素(包括 CRLF 对)的正确方法是使用\R
正则表达式元字符,在 v5.10 中引入。
The use encoding
pragma 已被强烈弃用。您应该使用use open
pragma,或在 3-arg 的模式参数中使用编码open
,或使用binmode
.
use v5.10; # minimal Perl version for \R support
use utf8; # source is in UTF-8
use warnings qw(FATAL utf8); # encoding errors raise exceptions
use open qw(:utf8 :std); # default open mode, `backticks`, and std{in,out,err} are in UTF-8
while (<>) {
s/\R\z//;
...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)