Use of PCRE regular expressions with MySQL Use of PCRE regular expressions with MySQL database database

Use of PCRE regular expressions with MySQL


MySQL UDF

lib_mysqludf_preg is a library of mysql UDFs (user-defined-functions) that provide access to the PCRE (perl compatible-regular-expressions) library for pattern matching.


MariaDb and PHP do not implement PCRE exactly the same way:

  • no delimiters in MariaDB
  • options are given at the beginning this way (?options)

I implemented it there https://github.com/jclaveau/php-logical-filter/blob/master/src/Rule/RegexpRule.php#L57-L82

/** * Removes the delimiter and write the options in a MariaDB way. * * @param  string The pattern written in a PHP PCRE way * @return string The pattern in a MariaDB syntax * * @todo   Find more difference between MariaDB and PHP and handle them. * @see    https://mariadb.com/kb/en/library/pcre/ */public static function php2mariadbPCRE($php_regexp){    $delimiter        = substr($php_regexp, 0, 1);    $quoted_delimiter = preg_quote($delimiter, '#');    if (!preg_match("#^$quoted_delimiter(.*)$quoted_delimiter([^$quoted_delimiter]*)$#", $php_regexp, $matches)) {        throw new \InvalidArgumentException(            "The provided PCRE regular expression (with the delimiter '$delimiter') cannot be parsed: "            .var_export($php_regexp, true)        );    }    $pattern = $matches[1];    $options = $matches[2];    return ($options ? "(?$options)" : '') . $pattern;}

There are probably much more differences to deal with but, at least, this is a start :)

Feel free to notice me here if you find some and want your case to be added and tested: https://github.com/jclaveau/php-logical-filter/issues