src/Request/Formatter/StringFormatter.php
protected function filterByRegex($value, $rule)
    {
        if (!isset($rule['regex']) || empty($rule['regex'])) {
            return;
        }

        //如果你看到此行报错，说明提供的正则表达式不合法
        if (preg_match($rule['regex'], $value) <= 0) {
            throw new BadRequest(
                T('{name} can not match {regex}',
                    ['name' => $rule['name'], 'regex' => $rule['regex']])
            );
        }
    }
protected function filterByRegex($value, $rule)
    {
        if (!isset($rule['regex']) || empty($rule['regex'])) {
            return;
        }

        //如果你看到此行报错，说明提供的正则表达式不合法
        if (preg_match($rule['regex'], $value) <= 0) {
            throw new BadRequest(
                T('{name} can not match {regex}',
                    ['name' => $rule['name'], 'regex' => $rule['regex']])
            );
        }
    }
进行正则匹配

@param $value
@param $rule
@throws BadRequest