是否有办法在生产服务器上使用MySQL数据库可靠地启用和使用全功能PCRE正则表达式,即使用捕获组,模式修饰符(区分大小写/不敏感,多行),元字符,转义序列(\s
,\w
)和其他PCRE好东西?
答案 0 :(得分:4)
lib_mysqludf_preg是一个mysql UDF库(用户定义的函数) 提供对PCRE的访问(perl compatible-regular-expressions) 用于模式匹配的库。
答案 1 :(得分:3)
切换到MariaDB。从10.0.5开始,MariaDB支持PCRE https://mariadb.com/kb/en/mariadb/pcre/
答案 2 :(得分:0)
MariaDb和PHP并不完全相同地实现PCRE:
我在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;
}
可能有更多的差异需要解决,但至少这是一个开始:)
如果您发现有一些问题并希望添加并测试您的案子,请随时在这里通知我:https://github.com/jclaveau/php-logical-filter/issues