[英]Checking MySQL Query value for '%' character with Regex
我想檢查MySQL Query的值是否包含%
字符(可以多次出現)但不包含轉義的\\%
。
所以它應該匹配:
'%test'
'%test\\%'
'test\\\\%'
- 應該匹配,因為它逃脫了\\
而不是%
'%test\\%%'
'\\%test%test\\%'
但不是:
'test\\%'
'\\%test\\%'
'\\%test\\%test\\%'
我在正則表達方面不擅長,有人可以幫助我嗎?
提前致謝。
編輯 :
我正在嘗試創建一個用於從數組生成SQL查詢的PHP庫,並且生成的查詢可能主要用於MySQL。
因此,正則表達式過濾過程將使用PHP。
對不起,我忘了提這個。
謝謝
假設SQL Server,這應該工作:
WHERE
ColumnX LIKE '%[%]%' ESCAPE '\'
雖然我不確定它是否正確處理\\\\%
。
這應該適用於任何情況:
WHERE
REPLACE(REPLACE(ColumnX, '\\', ''), '\%', '') LIKE '%[%]%'
在PHP代碼上檢查:
unset($str1,$str2,$str3,$str4,$str5,$str6,$str7);
$str1 = '%100';
$str2 = '%100\%';
$str3 = "100\\\\%";
$str4 = "%100\\%%";
$str5 = '100\%';
$str6 = "100\\\\\\\\%";
$str7 = "100\\\\\\%";
$str8 = "100\\\\\\\\\\\\%";
$str9 = "100\\\\\\\\\\\\\\%";
$reg_exp= '/^%|[^\x5C]%|[^\x5C](\x5C\x5C)+%/';
echo $str1.' = '.preg_match($reg_exp, $str1).'<br />';
echo $str2.' = '.preg_match($reg_exp, $str2).'<br />';
echo $str3.' = '.preg_match($reg_exp, $str3).'<br />';
echo $str4.' = '.preg_match($reg_exp, $str4).'<br />';
echo $str5.' = '.preg_match($reg_exp, $str5).'<br />';
echo $str6.' = '.preg_match($reg_exp, $str6).'<br />';
echo $str7.' = '.preg_match($reg_exp, $str7).'<br />';
echo $str8.' = '.preg_match($reg_exp, $str8).'<br />';
echo $str9.' = '.preg_match($reg_exp, $str9).'<br />';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.