Help with variable with regex inside.

T

timb

Hello.
I am writing a perl program that takes in a config file with different
regular expressions and parameters to parse dates. The date is in the
format of 2006/12/03 and I want it in the format of 12/03/2006 so I can
split it into the $month, $day, and $year


Config file:

TitleRegex = ^(\d{4})\/(\d{2})\/(\d{2})
DateRegex = $2/$3/$1


Config loading method:

open (CONFIG, "configfile.config") ||
die "Couldn't load config file for some reason";

while (<CONFIG>) {
chomp; # no newline
s/#.*//; # no comments
s/^\s+//; # no leading white
s/\s+$//; # no trailing white
next unless length; # anything left?
my ($var, $value) = split(/\s*=\s*/, $_, 2);
no strict 'refs';
$$var = $value;
}
close(CONFIG);


Date parsing subroutine:

sub getdate {
$rsstitle =~ m/$RssTitleRegex/ig;
eval { $DateRegex =~ s/(\$\w+)/$1/eeg };
die if $@;
($month, $day, $year) = split(/\//, $DateRegex);
}
 
M

Martijn Lievaart

Hello.
I am writing a perl program that takes in a config file with different
regular expressions and parameters to parse dates. The date is in the
format of 2006/12/03 and I want it in the format of 12/03/2006 so I can
split it into the $month, $day, and $year

You could split it into ($day, $month, $year). The position of a named
variable is irrelevant.
Config file:

TitleRegex = ^(\d{4})\/(\d{2})\/(\d{2})
DateRegex = $2/$3/$1

That is not a regex, it is a substitution pattern.
Config loading method:

open (CONFIG, "configfile.config") ||
die "Couldn't load config file for some reason";

die "Couldn't load config file: $!";
while (<CONFIG>) {
chomp; # no newline
s/#.*//; # no comments
s/^\s+//; # no leading white
s/\s+$//; # no trailing white
next unless length; # anything left?
my ($var, $value) = split(/\s*=\s*/, $_, 2);
no strict 'refs';
$$var = $value;
}
close(CONFIG);


Date parsing subroutine:

sub getdate {
$rsstitle =~ m/$RssTitleRegex/ig;

Did you mean TitleRegex? And what is $rsstitle?
eval { $DateRegex =~ s/(\$\w+)/$1/eeg };
die if $@;
($month, $day, $year) = split(/\//, $DateRegex);
}

It is completely unclear what you want to accomplish, but it looks like
you are going about in a horribly complicated way. Wouldn't

sub getdate {
($day, $month, $year) = split(/\//, $rsstitle);
return ($month, $day, $year);
}

also accomplish what you want?

M4
 
T

tim

Sorry for making it more complicated than is should be. The code I
posted are just the relivant snippets. The problem with your solution:
Wouldn't

sub getdate {
($day, $month, $year) = split(/\//, $rsstitle);
return ($month, $day, $year);
}

also accomplish what you want?

M4

is that I want to be able to use a variable to determine the input
format of the date.
-Tim
 
M

Martijn Lievaart

Sorry for making it more complicated than is should be. The code I
posted are just the relivant snippets. The problem with your solution:


is that I want to be able to use a variable to determine the input
format of the date.

Aha! In that case I would make the input like:

dateformat = %M/%D/%Y

and process that, but it would take some writing to accomplish that.

Now back to your problem. In your code, you do two evals. I think you
should try with one. Either use /ee on the substitution or use an eval. I
would go for the latter. Or even none at all.

(untested)

DateRegex = ^(\d{4})\/(\d{2})\/(\d{2})$
DateFormat = %Y/%M/%D

sub getdate {
my $date = shift; # give the date as a parameter
my ($year, $month, $day);
$date =~ m/$DateRegEx/;
my @t = ($1, $2, $3);
for (split(/\//, $DateFormat) {
/%D/ and do { $day = shift @t; next };
/%M/ and do { $month = shift @t; next };
/%Y/ and do { $year = shift @t; next; };
die "Bad format: $DateFormat";
}
return ($month, $day, $year);
}

This probably can be done shorter and more elegantly. But this should work.

HTH,
M4
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top