|
Posted by Toby Inkster on 10/10/05 21:57
BernieH wrote:
> I need a regex which will extract the year only from a variety of date
> strings such as -
>
> April 8, 2000
> 13 May 1999
> April 1999
> 1999
> 20050908 (yyyymmdd format)
It's unlikely a regexp would be able to do that. If you're using PHP,
check out the strtotime() function.
<?php
$testdates = array (
"April 8, 2000",
"13 May 1999",
"April 1999",
"1999",
"20050908",
"Next Tuesday",
"Yesterday",
"6 years"
);
while ( $somedate = array_pop($testdates) )
{
$year = (int)date("Y", $somedate);
print "<p>DATE is $date<br>YEAR is $year</p>\n";
}
?>
If you really, really need to use a regexp, the following will match a
year in many cases:
/[12][0-9]{3}/
but it won't be 100% reliable.
--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
[Back to original message]
|