AW: Regular expression for years after a given one

"Winter Christian" <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
Jose Alves de Castro wrote:
> Yesterday, a friend of mine was toying around with some Perl 
> code, when he got to a function which would only accept a 
> regular expression. He
> said:
> 
> "How can I say that I want every single year after the one I 
> have? It's impossible..."
> 
> To which I replied:
> 
> "Nothing is impossible! Especially in Perl! ;-) " (ok, maybe 
> these weren't my words at the time, but they seem cool now, 
> right? :-) )
> 
> So after a couple of minutes, this is what I came up with:
> 
> 
> $year = 2004; # I'm stating it directly to improve clarity
> 
> $year =~/(\d)(\d)(\d)(\d)/;
> 
> ($c,$d,$e,$f) = ($4+1,$3+1,$2+1,$1+1);
> 
> $regex =
> qr/^\d*(?:${1}${2}${3}[$c-9]|${1}$2[$d-9]\d|$1[$e-9]\d{2}|[$f-9]\d{3})$/;

Another approach:

use re 'eval';
my $re = qr/(\d{4})(??{ $1>$year?'':'x{1000}' })$/;

Assuming, of course, that $_ never contains a smaller year
number followed by thousand 'x'es. :-)

-Christian
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.