[英]Use of PCRE regular expressions with MySQL
有沒有辦法在生產服務器上使用MySQL數據庫可靠地啟用和使用全功能PCRE正則表達式,即使用捕獲組,模式修飾符(區分大小寫/不敏感,多行),元字符,轉義序列( \\s
, \\w
)和其他PCRE好東西?
lib_mysqludf_preg是一個mysql UDF(用戶定義函數)庫,提供對PCRE(perl compatible-regular-expressions)庫的訪問以進行模式匹配。
切換到MariaDB。 從10.0.5開始,MariaDB支持PCRE https://mariadb.com/kb/en/mariadb/pcre/
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.