ZF-10201: Zend_Validate_Regex will fail for certain valid regular expressions


Have created a form using the following element

$this->addElement("password", "pass_word", array(
  array("Regex", true, array("[a-zA-Z0-9~!@$%^&*]{5,15}"))

I get the following error rendered in my browser

"Internal error while using the pattern '[a-zA-Z0-9~!@$%^&*]{5,15}'"

The regular expression matches correctly if I use a java pattern matching tool (Part of jEdit)

I have tracked the fault back to Zend/Validate/Regex.php Line 107

 * Sets the pattern option
 * @param  string $pattern
 * @throws Zend_Validate_Exception if there is a fatal error in pattern matching
 * @return Zend_Validate_Regex Provides a fluent interface
public function setPattern($pattern)
 $this->_pattern = (string) $pattern;
 $status         = @preg_match($this->_pattern, "Test"); **** This is line 107 ****
 if (false === $status) {
  require_once 'Zend/Validate/Exception.php';
  throw new Zend_Validate_Exception("Internal error while using the pattern '$this->_pattern'");

 return $this;

As you can see the subject of preg_match will never pass my regular expression. I know that workarounds exist. I could daisy chain the regex validator with the stringLength validator or I could alter my regex


Your regex pattern doesn't have any delimiters so it is invalid. That's why the exception is thrown. The preg_match function will only return false if the pattern is incorrect. It returns 0 if no match was found.

This is not a bug. Read for more info on regular expressions in PHP.

I know. I had posted the bug by the time I had realised why it was returning false, and couldn't find a way to resolve the bug myself

Closing as non issue according to the responses

Correct fix version as it has been deleted unintentionally by another user