Zend_Validator_Regex抛出错误:使用模式时出现内部错误

Message: Internal error while using the pattern "(http://)?(www.)?(youtu)((be.com)|(.be))/.*"

$element = new Zend_Form_Element_Text($this->name);
$element->setRequired(true)
        ->setLabel(FileTypes::$names[$this->fileType])
        ->setDescription('Paste YouTube link here')
        ->setDecorators(FormDecorators::$simpleElementDecorators)
        ->addValidator('regex', false, '(http://)?(www\.)?(youtu)((be\.com)|(\.be))/.*');

Throws error even with simple regular expression.

Have you validated your regex as correct? Try it out in a regex tool, see if it generates any errors. A decent tool should tell you why your regex is wrong if it is invalid.

Most regexes I've seen are usually bookended with some kind of character, commonly '/'. The fact that yours isn't might have something to do with the error you're getting.

You should also bear in mind that whilst PHP's regex is similar to Perl, there are a few differences. They probably don't matter in this case, but you should be aware of them nonetheless. http://www.php.net/manual/en/reference.pcre.pattern.differences.php

try this, you need to put your pattern in an array and include the delimiters.

$element = new Zend_Form_Element_Text($this->name);
                $element->setRequired(true)
                        ->setLabel(FileTypes::$names[$this->fileType])
                        ->setDescription('Paste YouTube link here')
                        ->setDecorators(FormDecorators::$simpleElementDecorators)
                        ->addValidator('regex', false, array(
                           'pattern' => '/(http://)?(www\.)?(youtu)((be\.com)|(\.be))/.*/'                                                                    
                         ));

Escape the slashes in http:

->addValidator('regex', false, '(http:\/\/)?(www\.)?(youtu)((be\.com)|(\.be))\/.*');