-3

我这里有 2 种不同的文本格式。

"Submitted on Oct 1st, 2013"
"Not started" 

我想去掉状态和日期。

预期结果是:

$status = "Submitted" or "Not started"
$date = "Oct 1st, 2013"

如何在Perl. 非常感谢。

4

2 回答 2

1

一种从单个 RegEx 开始的方法。处理意外的输入。

#!/usr/bin/perl -w

use strict;
use warnings;

my ($match, $status, $date);
foreach (<DATA>) {

    $_ =~ /^"(Submitted)(?: on )(.*)"|(Not started)"/;

    #         ^^^^^^^^^          ^^    ^^^^^^^^^^^
    #            $1              $2        $3

    if (defined $1) {
        ($match, $status, $date) = ("Y", $1, $2);
    } elsif (defined $3) {
        ($match, $status, $date) = ("Y", $3, "-");
    } else {
        ($match, $status, $date) = ("N", "-", "-");
    }

    print "[", join("][", ($match, $status, $date)), "]\n";
}

__DATA__
"Submitted on Oct 1st, 2013"
"Not a match!"
"Not started"

该程序产生输出:

[Y][Submitted][Oct 1st, 2013]
[N][-][-]
[Y][Not started][-]
于 2013-10-05T02:52:40.980 回答
1

If you can assume that there is always the word "on" before the date, here's the code that will do the thing.

#!/usr/bin/perl

use strict;
use warnings;

chomp(my $input = <STDIN>);

my $status = "Not started";
my $date;

if ($input =~ / on /) {
    $date = $';
    $status = "Submitted";
}

print "Status: $status\n";
if (defined $date) {
    print "Date: $date\n";
}
于 2013-10-05T00:46:15.867 回答