Route regex for date - capturing group

I’m trying to add regex to the route.
My idea is to check for correct date format - yyyy-mm-dd.
I want to call my api like so:

below is my test code:

$app->get(’/search/{from:[0-9]{4}-(0[1-9]|1[0-2])-[0-9]{2}}/{to:[0-9]{4}-(0[1-9]|1[0-2])-[0-9]{2}}’, function (Request $request, Response $response, $args) {
$from = $args[‘from’];
$to = $args[‘to’];
$response->getBody()->write($from." - ".$to);
return $response;

unfortunately, I get error about capturing group:

{“error”:“Regex “[0-9]{4}-(0[1-9]|1[0-2])-[0-9]{2}” for parameter “from” contains a capturing group”}

I’m aware that this isn’t supported (, but maybe there is a way to rewrite that regex, if yes, how should it look like?

Apply different logic …

In the route, use a simple from:[12]\d{3}\-\d{2}\-\d{2} type check, and then in the route controller use the library - a validator of the vlucas/valitron type.

Thank you for a quick reply.
I was thinking about using regex, but validator is even better.

What would be the easiest way of using this inside my code?

Right now I’m using Validator like so:

$app->get('/search/{from:[12]\d{3}\-\d{2}\-\d{2}}/{to:[12]\d{3}\-\d{2}\-\d{2}}', function (Request $request, Response $response, $args) {
    $from = $args['from'];
    $to = $args['to'];

    $v = new Validator(['from' => $from, 'to' => $to]);
        'dateFormat' => [
            ['from', 'Y-m-d'], ['to', 'Y-m-d']
        'dateAfter' => [
            ['from', date("Y-m-d",strtotime("-1 days"))]

    if (!$v->validate()) {
        // Errors
    } else {
        $response->getBody()->write($from . " - " . $to);

    return $response;

@Misiu your implementation is ok if you is using Validator for this route only. In the future if you find yourself writing more stuff like this you could rewrite for a better implementation.

But the thing I noticed is your route pattern, for me wasn’t clear that your were checking dates between a period. Using the dates as query params wouldn’t be better? Something like: /api/check?from=2019-12-05&to=2019-12-10?

I’m just bringing this to topic putting myself as consumer of the API.

I changed my route to pass params using GET (as You suggested :wink:).
This works fine with Validator