I have a file format which is similar to "IDY03101.200901110500.axf". I have about 25 similar files residing in an ftp repository and want to download all those similar files only for the current date. I have used the following code, which is not working and believe the regular expression is incorrect.
我的文件格式类似于“IDY03101.200901110500.axf”。我有大约25个类似的文件驻留在ftp存储库中,并希望仅为当前日期下载所有这些类似的文件。我使用了以下代码,这是无效的,并认为正则表达式不正确。
my @ymb_date = "IDY.*\.$year$mon$mday????\.axf"; foreach my $file ( @ymb_date) { print STDOUT "Getting file: $file\n"; $ftp->get($file) or warn $ftp->message; }
Any help appreciated.
任何帮助赞赏。
EDIT:
I need all the current days file. using ls -lt | grep "Jan 13"
works in the UNIX box but not in the script What could be a vaild regex in this scenario?
我需要所有当前日期文件。使用ls -lt | grep“Jan 13”在UNIX框中工作,但不在脚本中。在这种情况下,什么可能是vaild正则表达式?
2 个解决方案
#1
It doesn't look like you're using any regular expression. You're trying to use the literal pattern as the filename to download.
它看起来不像你正在使用任何正则表达式。您正在尝试使用文字模式作为要下载的文件名。
Perhaps you want to use the ls
method of Net::FTP to get the list of files then filter them.
也许你想使用Net :: FTP的ls方法来获取文件列表然后过滤它们。
foreach my $file ( $ftp->ls ) {
next unless $file =~ m/$regex/;
$ftp->get($file);
}
You might also like the answers that talk about implementing mget
for "Net::FTP" at Perlmonks.
您可能也喜欢在Perlmonks上讨论为“Net :: FTP”实现mget的答案。
Also, I think you want the regex that finds four digits after the date. In Perl, you could write that as \d{4}
. The ?
is a quantifier in Perl, so four of them in a row don't work.
另外,我想你想要在日期之后找到四位数的正则表达式。在Perl中,您可以将其写为\ d {4}。的?是Perl中的量词,因此连续四个不起作用。
IDY.*\.$year$mon$mday\d{4}\.axf
#2
I do not think regexes work like that. Though it has been awhile since I did Perl, so I could be way off there. :)
我不认为正则表达式是这样的。虽然我做了Perl已经有一段时间了,所以我可以离开那里。 :)
Does your $ftp
object have access to an mget()
method? If so, maybe try this?
你的$ ftp对象是否可以访问mget()方法?如果是这样,也许试试这个?
$ftp->mget("IDY*.$year$mon$mday*.axf") or warn $ftp->message;
#1
It doesn't look like you're using any regular expression. You're trying to use the literal pattern as the filename to download.
它看起来不像你正在使用任何正则表达式。您正在尝试使用文字模式作为要下载的文件名。
Perhaps you want to use the ls
method of Net::FTP to get the list of files then filter them.
也许你想使用Net :: FTP的ls方法来获取文件列表然后过滤它们。
foreach my $file ( $ftp->ls ) {
next unless $file =~ m/$regex/;
$ftp->get($file);
}
You might also like the answers that talk about implementing mget
for "Net::FTP" at Perlmonks.
您可能也喜欢在Perlmonks上讨论为“Net :: FTP”实现mget的答案。
Also, I think you want the regex that finds four digits after the date. In Perl, you could write that as \d{4}
. The ?
is a quantifier in Perl, so four of them in a row don't work.
另外,我想你想要在日期之后找到四位数的正则表达式。在Perl中,您可以将其写为\ d {4}。的?是Perl中的量词,因此连续四个不起作用。
IDY.*\.$year$mon$mday\d{4}\.axf
#2
I do not think regexes work like that. Though it has been awhile since I did Perl, so I could be way off there. :)
我不认为正则表达式是这样的。虽然我做了Perl已经有一段时间了,所以我可以离开那里。 :)
Does your $ftp
object have access to an mget()
method? If so, maybe try this?
你的$ ftp对象是否可以访问mget()方法?如果是这样,也许试试这个?
$ftp->mget("IDY*.$year$mon$mday*.axf") or warn $ftp->message;