Generating PCRE Regular Expressions from Date Format Strings in PHP

Have you ever found yourself wishing that it was as easy to write a regular expression to match a time/date string as it is to generate a time/date string with the PHP date() function?

Do you find yourself in need of a quick and easy way to "tokenize" time/date strings like "September 7th, 2010" into values like "9", "7", and "2010"?

Well, I'm proud to announce that we here at RedBottle have the solution for your problems, and it's called DateFormatRegexGenerator.

Without further ado, here's an example to illustrate its power:

$dateString = "September 7th, 2010";

$regexGenerator = DateFormatRegexGenerator::getInstance();

/* Generates the following regular expression:
 *
 * /^(?:January|February|March|April|May|June|July|August|September|October|November
 * |December) (?:[1-9]|[1-2][0-9]||3[0-1])(?:st|nd|rd|th), (?:[0-9]{4})$/
 */
$regex = $regexGenerator->generateRegex('F jS, Y', TRUE);

preg_match($regex, $dateString, $matches);

print_r($matches);

This will print:

Array
(
  [0] => September 7th, 2010
)

The DateFormatRegexGenerator class supports converting any date format string that can be passed to the PHP date() function into a regular expression that can be used to match date/time strings of that format. This makes generation of date-and-time-related regular expressions trivial.

In addition, CapturingDateFormatRegexGenerator, a sub-class of DateFormatRegexGenerator, can be used to generate "capturing" regular expressions that capture each part of the date string, making it easy to both validate and tokenize date/time strings.

Here's an example that's similar to the one above, but more powerful:

$dateString = "September 7th, 2010";

$regexGenerator = CapturingDateFormatRegexGenerator::getInstance();

/* Generates the following regular expression:
 *
 * /^(January|February|March|April|May|June|July|August|September|October|November
 * |December) ([1-9]|[1-2][0-9]||3[0-1])(st|nd|rd|th), ([0-9]{4})$/
 */
$regex = $regexGenerator->generateRegex('F jS, Y', TRUE);

preg_match($regex, $dateString, $matches);

print_r($matches);

This will produce the following output:

Array
(
  [0] => September 7th, 2010
  [1] => September
  [2] => 7
  [3] => th
  [4] => 2010
)

You can get these handy utility classes, along with their corresponding PHPUnit unit tests, from our website. The code is licensed under version 3 of the Lesser GNU General Public License.

If you find any bugs or have any questions, feel free to post a comment.