I have 2 different text formats here.
"Submitted on Oct 1st, 2013"
"Not started"
I want to strip the status and the date.
Expected result is:
$status = "Submitted" or "Not started"
$date = "Oct 1st, 2013"
How to do it in Perl. Many thanks.
I have 2 different text formats here.
"Submitted on Oct 1st, 2013"
"Not started"
I want to strip the status and the date.
Expected result is:
$status = "Submitted" or "Not started"
$date = "Oct 1st, 2013"
How to do it in Perl. Many thanks.
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";
}
An approach that begins with a single RegEx. Handles unexpected inputs.
#!/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"
This program produces the output:
[Y][Submitted][Oct 1st, 2013]
[N][-][-]
[Y][Not started][-]