Re: Regex help

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.

$testdates = array (
"April 8, 2000",
"13 May 1999",
"April 1999",
"Next Tuesday",
"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:


but it won't be 100% reliable.

Toby A Inkster BSc (Hons) ARCS
Contact Me ~


Relevant Pages

  • RE: [PHP] Re: question regarding form filtering
    ... And, actually, the PHP check might be more involved than the JS check. ... the validation in JS may not always be exactly the same ... contextual help via javascript info popups. ... Ok so my reason being for using regexp is that by defining a regexp my ...
  • Re: attempting to return values from array from w/in a function
    ... What I get is an array w/ only the most recent array entry. ... I am new to PHP & do not have formal scripting ... you end up comparing strings, but the test you mean probably is simply: ... case 1: {codeblock} ...
  • Re: Regular expression generator/creator
    ... Have you checked out the latest version of Chainsaw? ... >> whose regexp matches. ... > In a perl script I wrote some time ago, I'm grouping log messages by comparing ... > If I read the module's code correctly, the strings are split up by whitespace ...
  • Re: [PHP] pack it in
    ... that's where my brain goes to mush - all strings in php ... in php and such found in these 'binary files' that they need to be ... packallows you to format binary data. ... # Do the conversion the way that the system architecture wants to ...
  • Re: Storing condition in bitfield?
    ... which I would like to bind form field's validation scheme/condition. ... I believe that in PHP, you can actually store PHP code in strings, and ... opposed to, say, an XML or ASCII representation. ... Putting strings and XML will be of overhead, ...