RE: Regular expression for years after a given one

"Allen, Greg" <[email protected]>
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
Think it might be bust when year contains a 9.

e.g. year = 2009

$c = 9+1 = 10

qr/^\d*(?:${1}${2}${3}[$c-9]...
is:
qr/^\d*(?:200[10-9]...

and [10-9] matches any digit, so 2000, 2001 etc. would incorrectly pass.

Greg


-----Original Message-----
From: Jose Alves de Castro [mailto:[email protected]] 
Sent: Tuesday, June 01, 2004 10:53 AM
To: [email protected]
Subject: Regular expression for years after a given one


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})$/;


and that does the trick :-)

Thought I should share this with you guys :-) Thoughts are welcome :-)


BTW: This was made so it would work with $year consisting of four
digits... any care to make it generic? :-)


Regards,

jac

-- 
José Alves de Castro <[email protected]>
Telbit - Tecnologias de Informação



------------------------------------------------------------------------------
This message is intended only for the personal and confidential use of the designated recipient(s) named above.  If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited.  This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers.  Email transmission cannot be guaranteed to be secure or error-free.  Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such.  All information is subject to change without notice.
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.