Thomas Clowes
Thomas Clowes

Reputation: 4609

PHP Regular Expression - not working.. should be

I am trying to extract dates from a text variable.

I have created a regex which extracts 'MOST' formats of date as follows:

    $regexp = '#[0-9]{2,4}[-\/ ]{1}([A-Za-z]{3}|[0-9]{2})[-\/ ]{1}[0-9]{2,4}#';
preg_match_all($regexp, $output, $dates);

It does not however extract dates of the format '08 Aug 2012' and I do not know why.. As far as I can tell.. it should..

For now I have inserted a seperate regex which works:

    $regexp = '#[0-9]{2}[ ]{1}[A-Za-z]{3}[ ]{1}[0-9]{4}#';
preg_match_all($regexp, $output, $dates);

which is essentially the same..

It however seems pointless to have multiple regex when I need only have one.

If anyone could tell me why the first regex isnt working for such a format, and explain why, it would be greatly appreciated.

Thanks

Upvotes: 0

Views: 75

Answers (1)

Chronial
Chronial

Reputation: 70673

Well, your regexp is correct for the date format you presented. And as such it also works without problems: http://ideone.com/XxdKV

Upvotes: 2

Related Questions