【发布时间】:2010-12-11 01:26:30
【问题描述】:
我被这个正则表达式困住了。它匹配我的 3 个文件名中的 2 个。如果可能,需要帮助获得所有三个。
我还想将扩展名.edu | .net 之前的abc|def|ghi 和ucsb|tech 语言环境名称之一提取到变量中。
如果可能,希望一次性完成。谢谢。
/home/test/abc/.last_run_dir
/home/test/def/.last_file_sent.mail@wolverine.ucsb.edu
/home/test/ghi/.last_file_sent.dp3.tech.net
它没有拿起第一行:
/home/test/abc/.last_run_dir
正则表达式:
$line =~ m#home/test/(\w{3}).*[.](\w+)[.].*#
代码:
my $file = 'Index.lst';
open my $FILE, '<', $file or die "unable to open '$file' for reading: $!";
while (my $line = <$FILE>) {
chomp($line);
if ($line =~ m#home/test/(\w{3}).*[.](\w+)[.].*#) {
open my $file2, '<', $line or die "unable to open '$file' for reading: $!";
while(my $line2 = <$file2>) {
print "$line2";
}
close $file2;
}
} #end while
close $FILE;
另外,我如何打印出我可能的匹配项?如果它们是可选的?
【问题讨论】:
标签: regex perl pattern-matching