【发布时间】:2012-03-17 15:22:17
【问题描述】:
我有一个从文本文件中读取的字符串,但在 Ubuntu Linux 中,我尝试从末尾删除它的换行符。
我用过所有的方法。但是对于s/\n|\r/-/(我看看它是否找到任何替换任何新行字符串)它会替换字符串,但是当我打印它时它仍然会转到下一行。此外,当我使用chomp 或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);
【问题讨论】:
-
@TLP 请不要假装 Perl 字符类有 ASCII 定义,因为这在 Perl 中是完全错误的。你必须使用the definitions from UTS#18 Annex C。
-
@TLP 是的,当然不是。
\w等于[\p{Alphabetic}\p{Mark}\p{Decimal_Number}\p{Connector_Punctuation}]。这是众所周知的。它涵盖了截至 Unicode v6.0 的 102,724 个代码点,比您提到的不足 63 个多 四个数量级。