是否有一种方法可以在生产服务器上与MySQL数据库可靠地启用和使用全功能的PCRE正则表达式,即使用捕获组,模式修饰符(大小写敏感/不敏感,多行),元字符,转义序列('s
, 'w
)和其他PCRE好东西?
MySQL UDF
lib_mysqludf_preg是一个mysqludf(用户定义函数)库。提供对PCRE (perl compatible-regular-expressions)的访问模式匹配库。
切换到MariaDB。从10.0.5开始,MariaDB支持PCRE https://mariadb.com/kb/en/mariadb/pcre/
MariaDb和PHP实现PCRE的方式不完全相同:
- MariaDB中没有分隔符
- 选项在开头以这种方式给出(?options)
我在那里实现了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