我寫了一個Perl程序,它從文本文件中讀取文本並將其打印出來。Perl正則表達式問題
我想打印出一個具有特定格式的行。
例如,有這樣的句子:
information:
Ahmad.prn:592118:2001:7:5:/Essay
Ashford.rtf:903615:2001:6:28:/usr/Essay
Barger.doc:243200:2001:7:4:/home/dir
end of Information.
我只想閱讀這些三行:
Ahmad.prn:592118:2001:7:5:/Essay
Ashford.rtf:903615:2001:6:28:/usr/Essay
Barger.doc:243200:2001:7:4:/home/dir
我認爲字段的含義是:
Ahmad.prn <- file name
592118 <- size of file
2001:7:5 <- created date
/Essay <- path of file
我的代碼是這樣的:
#!/usr/bin/perl
use strict;
use warnings;
open (my $infh, "<", $file)||die "cant open";
while(my $line = <$infh>) {
chomp ($line);
if ($line =~ /(what regular expression do I have to put in here?)/) {
print "$line";
}
}
close ($infh);
你確實需要提供更多關於你想要實現的信息。 – dalton 2011-03-05 12:12:13
請清楚說明線路格式是什麼。目前還不清楚你是否只想刪除第一行和最後一行,只匹配以/ Essay結尾的行,匹配匹配a:b:c:d:e:f ....的行。 – Mat 2011-03-05 12:20:49