微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

正则表达式 – 使用Perl验证年份范围

这些是源文件标题中的有效年份,版权行:

2016
2007,2016
2007-2010,2016
2010,2012-2016

本年度(2016年)应该是其中的一部分.

无效:

2016,2007 # not in order
2010-2007,2016 #range not in order.
2010-2015 # current year missing.

我制作了如下脚本,仍然在努力,是否有更好的方法来检查相同的?

use strict;
use warnings;
use List::Util 'first';
use Time::Piece;

my $t = Time::Piece->new();
my $currentYear=$t->year;

my $filename="sample.txt"; # this file name will be passed-in to the script.
my $fileContent=`cat $filename`;
my $copyrightFound = first { /copyright .* Shakespeare/i } $fileContent;
if (!$copyrightFound) {
     print "\nERROR: copyright missing\n";
     exit;
}
#copyright Found
print $fileContent;
if ($fileContent =~ /copyright \(c\) (.*) by Bill Shakespeare\s+.*All rights reserved./) {
    print "\ncopyright is good: $1\n";
    my $years = $1;
    print "\n$years,$currentYear\n";
    #e.g: 2016
    if ($years !~ ',' && $years !~ '-') {
       print "\nerror\n" if ($years !~ /$currentYear/);
    }
    #e.g: 2010-2016
    elsif ($years !~ ',') {
       print "\nError\n" if ($years !~ /\-$currentYear$/);
    }
    #eg: 2010,2016
    elsif ($years !~ '-') {
       print "\nError\n" if ($years !~ /\,$currentYear$/);
    }
    #e.g: 2008,2010,2011-2016
    elsif ($years =~ '-' && $years =~ ',') {
       print "\nError 5\n" if ($years !~ /\d\d\d\d,\d\d\d\d\-$currentYear$/);
    }
    else {
       print "invalid format"
    }

} else {
      print "\ncopyright needs to be fixed\n";
}

sample.txt有:

copyright (c) 2008,2011-2016 by Bill Shakespeare
All rights reserved.

解决方法

您可以使用 Set::IntSpan来验证日期:

use warnings;
use strict;
use Set::IntSpan qw();

my $currentYear = 2016;

while (<DATA>) {
    s/\s+//g;
    my $ok = 0;
    if (Set::IntSpan->valid($_)) { # Checks order
        my $set = Set::IntSpan->new($_);
        my @years = $set->elements();
        $ok = 1 if grep { $_ == $currentYear } @years;
    }
    print "$ok : $_\n";
}

__DATA__
2007
2007,2012-2016
2016,2007
2010-2007,2016
2010-2015

这打印:

0 : 2007
1 : 2007,2016
1 : 2007-2010,2016
1 : 2010,2012-2016
0 : 2016,2007
0 : 2010-2007,2016
0 : 2010-2015

正如pilcrow在评论中所建议的那样,简化使用成员而不是grep:

my $ok = 0;
if (Set::IntSpan->valid($_)) { 
    my $set = Set::IntSpan->new($_);
    $ok = 1 if $set->member($currentYear);
}
print "$ok : $_\n";

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐