我就廢話不多說了,大家還是直接看代碼吧~
實例
imos=# select 'hello' ~ '[\u2e80-\ua4cf]|[\uf900-\ufaff]|[\ufe30-\ufe4f]';
?column?
----------
f
(1 row)
imos=#
imos=# select 'hello中國' ~ '[\u2e80-\ua4cf]|[\uf900-\ufaff]|[\ufe30-\ufe4f]';
?column?
----------
t
(1 row)
補充:PostgreSQL 判斷字符串包含的幾種方法
判斷字符串包含的幾種方法:
1. position(substring in string):
postgres=# select position('aa' in 'abcd');
position
----------
0
(1 row)
postgres=# select position('ab' in 'abcd');
position
----------
1
(1 row)
postgres=# select position('ab' in 'abcdab');
position
----------
1
(1 row)
可以看出,如果包含目標字符串,會返回目標字符串笫一次出現的位置,可以根據返回值是否大于0來判斷是否包含目標字符串。
2. strpos(string, substring):
該函數的作用是聲明子串的位置。
postgres=# select strpos('abcd','aa');
strpos
--------
0
(1 row)
postgres=# select strpos('abcd','ab');
strpos
--------
1
(1 row)
postgres=# select strpos('abcdab','ab');
strpos
--------
1
(1 row)
作用與position函數一致。
3. 使用正則表達式:
postgres=# select 'abcd' ~ 'aa';
?column?
----------
f
(1 row)
postgres=# select 'abcd' ~ 'ab';
?column?
----------
t
(1 row)
postgres=# select 'abcdab' ~ 'ab';
?column?
----------
t
(1 row)
4. 使用數組的@>操作符(不能準確判斷是否包含):
postgres=# select regexp_split_to_array('abcd','') @> array['b','e'];
?column?
----------
f
(1 row)
postgres=# select regexp_split_to_array('abcd','') @> array['a','b'];
?column?
----------
t
(1 row)
注意下面這些例子:
postgres=# select regexp_split_to_array('abcd','') @> array['a','a'];
?column?
----------
t
(1 row)
postgres=# select regexp_split_to_array('abcd','') @> array['a','c'];
?column?
----------
t
(1 row)
postgres=# select regexp_split_to_array('abcd','') @> array['a','c','a','c'];
?column?
----------
t
(1 row)
可以看出,數組的包含操作符判斷的時候不管順序、重復,只要包含了就返回true,在真正使用的時候注意。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- PostgreSQL的中文拼音排序案例
- 自定義函數實現單詞排序并運用于PostgreSQL(實現代碼)
- PostgreSQL將數據加載到buffer cache中操作方法
- 在PostgreSQL中使用ltree處理層次結構數據的方法
- postgresql 中的時間處理小技巧(推薦)
- Postgresql限制用戶登錄錯誤次數的實例代碼
- PostgreSQL用戶登錄失敗自動鎖定的處理方案
- postgresql影子用戶實踐場景分析
- 如何使用PostgreSQL進行中文全文檢索