Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Help with variable with regex inside.

Reply
Thread Tools

Help with variable with regex inside.

 
 
timb
Guest
Posts: n/a
 
      12-04-2006
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);
}

 
Reply With Quote
 
 
 
 
tim
Guest
Posts: n/a
 
      12-04-2006
I forgot to add the out put of $DateRegex:
//$1
-Tim

timb wrote:
> 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);
> }


 
Reply With Quote
 
 
 
 
Martijn Lievaart
Guest
Posts: n/a
 
      12-04-2006
On Sun, 03 Dec 2006 23:05:31 -0800, timb wrote:

> 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
--
Redundancy is a great way to introduce more single points of failure.

 
Reply With Quote
 
tim
Guest
Posts: n/a
 
      12-04-2006
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

Martijn Lievaart wrote:
> On Sun, 03 Dec 2006 23:05:31 -0800, timb wrote:
>
> > 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
> --
> Redundancy is a great way to introduce more single points of failure.


 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      12-04-2006
On Mon, 04 Dec 2006 00:05:11 -0800, tim wrote:

> 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.


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
--
Redundancy is a great way to introduce more single points of failure.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments