PostgreSql 字符串匹配

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

一、概述

  PostgreSQL 提供了三种独立的实现模式匹配的方法SQL LIKE 操作符、更近一些的 SIMILAR TO 操作符SQL:1999 里添加进来的和 POSIX 正则表达式。虽然大部分的正则表达式搜索都能被很快地执行但是正则表达式仍可能被人为地设置成需要任意长的时间和任意量的内存进行处理如果必须这样做建议加上语句超时限制。使用 SIMILAR TO 模式的搜索具有同样的安全性危险LIKE 搜索比其他两种选项简单得多要更安全些。

二、SQL LIKE 操作符

2.1 语法

string LIKE pattern [ESCAPE escape-character]
string NOT LIKE pattern [ESCAPE escape-character]

参数说明

string待匹配的字符串。
pattern匹配规则。若不包含百分号或者下划线那么只匹配串本身这时 LIKE 的行为等价于等号操作符下划线_代表匹配任何单个字符百分号%匹配任何零或更多个字符。
ESCAPE自定义转义字符默认为反斜线\。

补充
PostgreSQL 还提供了标准 SQ L中没有的 ILIKE 操作符用于忽略大小写的模式匹配。

~~等效于 LIKE。
~~*等效于 ILIKE。
!~~等效于 NOT LIKE。
!~~*操作符 NOT ILIKE。

2.2 示例

--正常字符匹配
postgres=# select * from t;
 id |   name
----+----------
  1 | XIAOH
  2 | XIAOO
  3 | xiaoguo
  4 | xiaowan
  5 | xiaopiao
  6 | xiaopen
(6 rows)

postgres=# select * from t where name like 'xiao%';
 id |   name
----+----------
  3 | xiaoguo
  4 | xiaowan
  5 | xiaopiao
  6 | xiaopen
(4 rows)

postgres=# select * from t where name ~~ 'xiao%';
 id |   name
----+----------
  3 | xiaoguo
  4 | xiaowan
  5 | xiaopiao
  6 | xiaopen
(4 rows)

postgres=# select * from t where name not like 'xiao%';
 id | name
----+-------
  1 | XIAOH
  2 | XIAOO
(2 rows)

postgres=# select * from t where name !~~ 'xiao%';
 id | name
----+-------
  1 | XIAOH
  2 | XIAOO
(2 rows)

postgres=# select * from t where name ilike 'xiao%';
 id |   name
----+----------
  1 | XIAOH
  2 | XIAOO
  3 | xiaoguo
  4 | xiaowan
  5 | xiaopiao
  6 | xiaopen
(6 rows)

postgres=# select * from t where name ~~* 'xiao%';
 id |   name
----+----------
  1 | XIAOH
  2 | XIAOO
  3 | xiaoguo
  4 | xiaowan
  5 | xiaopiao
  6 | xiaopen
(6 rows)

postgres=# select * from t where name not ilike 'xiao%';
 id | name
----+------
(0 rows)

postgres=# select * from t where name !~~* 'xiao%';
 id | name
----+------
(0 rows)

--加转义匹配
postgres=# select * from t;
 id | name
----+------
  1 | \
  2 | \n
  3 | \\
  4 | a\a
(4 rows)

postgres=# select * from t where name like '\%';
 id | name
----+------
(0 rows)

postgres=# select * from t where name like '\\%';
 id | name
----+------
  1 | \
  2 | \n
  3 | \\
(3 rows)

postgres=# select * from t where name like '3\%' escape '3';
 id | name
----+------
  1 | \
  2 | \n
  3 | \\
(3 rows)

postgres=# select * from t where name like '#\%' escape '#';
 id | name
----+------
  1 | \
  2 | \n
  3 | \\
(3 rows)

postgres=# select * from t where name like '@\%' escape '@';
 id | name
----+------
  1 | \
  2 | \n
  3 | \\
(3 rows)

三、SIMILAR TO 操作符

3.1 语法

string SIMILAR TO pattern [ESCAPE escape-character]
string NOT SIMILAR TO pattern [ESCAPE escape-character]

参数说明

string待匹配的字符串。
pattern匹配规则。若不包含百分号或者下划线那么只匹配串本身这时 LIKE 的行为等价于等号操作符下划线_代表匹配任何单个字符百分号%匹配任何零或更多个字符。
ESCAPE自定义转义字符默认为反斜线\。

除了支持上述 LIKE 的语法外SIMILAR TO 还支持 POSIX 正则表达式匹配元字符

|表示选择两个候选之一。
*表示重复前面的项零次或更多次。
+表示重复前面的项一次或更多次。
?表示重复前面的项零次或一次。
{m}表示重复前面的项刚好m次。
{m,}表示重复前面的项m次或更多次。
{m,n}表示重复前面的项至少m次并且不超过n次。

可以使用圆括号()把多个项组合成一个逻辑项。一个方括号表达式[…]声明一个字符类就像 POSIX 正则表达式一样。

3.2 示例

postgres=# select * from t;
 id |           name
----+--------------------------
  1 | ab
  2 | cdcdcdcdcdcdcdcdcdcdcdcd
  3 | efghefghefghefgh
(3 rows)

postgres=# select * from t where name similar to 'c%';
 id |           name
----+--------------------------
  2 | cdcdcdcdcdcdcdcdcdcdcdcd
(1 row)

postgres=# select * from t where name similar to '(a|c)%';
 id |           name
----+--------------------------
  1 | ab
  2 | cdcdcdcdcdcdcdcdcdcdcdcd
(2 rows)

postgres=# select * from t where name similar to '(cd)*';
 id |           name
----+--------------------------
  2 | cdcdcdcdcdcdcdcdcdcdcdcd
(1 row)

postgres=# select * from t where name similar to '(cd)+';
 id |           name
----+--------------------------
  2 | cdcdcdcdcdcdcdcdcdcdcdcd
(1 row)

postgres=# select * from t where name similar to '(ab)?';
 id | name
----+------
  1 | ab
(1 row)

postgres=# select * from t where name similar to '(efghefgh){2}';
 id |       name
----+------------------
  3 | efghefghefghefgh
(1 row)

postgres=# select * from t where name similar to '(efgh){2,}';
 id |       name
----+------------------
  3 | efghefghefghefgh
(1 row)

四、POSIX 正则表达式

4.1 匹配操作符

操作符描述例子
text ~ text → boolean字符串匹配正则表达式大小写敏感‘thomas’ ~ ‘.*thom.*’ → t
text ~* text → boolean字符串匹配正则表达式大小写不敏感‘thomas’ ~* ‘.*Thom.*’ → t
text !~ text → boolean字符串不匹配正则表达式大小写敏感‘thomas’ !~ ‘.*thomas.*’ → f
text !~* text → boolean字符串不匹配正则表达式大小写不敏感‘thomas’ !~* ‘.*Thom.*’ → f

4.2 常用匹配规则

匹配规则符描述
|选择两个候选之一
*重复前面的项零次或更多次
+重复前面的项一次或更多次
()括号中内容一个逻辑项
?前面的项零次或一次
*?*的非贪婪模式
+?+的非贪婪模式
???的非贪婪模式
{m}重复前面的项刚好m次
{m,}重复前面的项m次或更多次
{m,n}重复前面的项至少m次并且不超过n次
{m}?{m}的非贪婪模式
{m,}?{m,}的非贪婪模式
{m,n}{m,n}的非贪婪模式
^匹配字串的开头
$匹配字串的结尾
\n匹配换行
\r匹配回车
\t匹配制表符
\d匹配任意数字
\s匹配空格
\w匹配任意字母
\D匹配非数字
\S匹配非空格
\W匹配非字母
[a-z]匹配任意小写字母
[A-Z]匹配任意大写字母
[^a-z]匹配非小写字母
[^A-Z]匹配非大写字母
[0-9]匹配任意数字
[^0-9]匹配非数字
[abc]匹配a或b或c或ab或ac或bc
[a]+匹配一个a或多个a

4.2 常用相关函数

--substring 函数匹配字符串
postgres=# select substring('foobar',3);
 substring
-----------
 obar
(1 row)

postgres=# select substring('foobar',3,2);
 substring
-----------
 ob
(1 row)

postgres=# select substring('foobar' from '..$');
 substring
-----------
 ar

--regexp_replace 函数替换字符串
postgres=# select regexp_replace('foobar','o','x');
 regexp_replace
----------------
 fxobar
(1 row)

postgres=# select regexp_replace('foobar','o','x','g');
 regexp_replace
----------------
 fxxbar
(1 row)

postgres=# select regexp_replace('foobar','b..','x');
 regexp_replace
----------------
 foox

--regexp_match 函数匹配返回文本数组
postgres=# select regexp_match('foobar','..b');
 regexp_match
--------------
 {oob}
(1 row)

postgres=# select regexp_match('foobar','(f)(..b)');
 regexp_match
--------------
 {f,oob}
(1 row)

--regexp_matches 函数匹配返回文本数组的集合
postgres=# select regexp_matches('foobarbequebazilbarfbonk','b[^b]+','g');
 regexp_matches
----------------
 {bar}
 {beque}
 {bazil}
 {barf}
 {bonk}
(5 rows)


--regexp_split_to_table 函数把一个 POSIX 正则表达式模式当作一个定界符来分离一个串
postgres=#  select regexp_split_to_table('shi yi shi','\s');
 regexp_split_to_table
-----------------------
 shi
 yi
 shi
(3 rows)
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

“PostgreSql 字符串匹配” 的相关文章

YOLOX改进之损失函数修改(上)

文章内容:如何在YOLOX官网代码中修改–置信度预测损失 环境:pytorch1.8 损失函数修改内容: (1)置信度预测损失更换:二元交叉熵损失替换为FocalLoss或者VariFocalLoss &#x...

Pro Android学习笔记(一二二):Telephony API(4):发Email

作者@恺风Wei。发Email不属于Telephony API,但作为常用通信的方式,也在此提及。Android通过intent调用Email App应用的方式进行短信发送。如果我们开发办公软件,Email还是常用的功能。代码例子如下: private void sendEmailTest(){...

spark本地安装教程

一、 VMware虚拟机的安装 简介:VMWare虚拟机软件是一个“虚拟PC”软件,它使你可以在一台机器上同时运行二个或更多Windows、LINUX等系统,本章主要介绍该软件的安装。 注意事项:这一步基本没什么难点,傻瓜式跟着教程下一步即可,唯一需要注意的点就是第5步中安装路径不要包含中文,可能会...

配置图

配置图(Deployment):显示运行软件系统的物理硬件,以及如何将软件配置到硬件上。显示运行时处理元素,以及相关软件进程的配置 组成元素: 节点:运行时代表计算资源的物理元素,它一般用于执行处理或计算的资源建模。 关系:依赖和关联 http://dl.i...

shared

#include <memory> #include <string> #include <iostream> using namespace std; int main() { shared_ptr<string> p = make_shared...

php报错“ocienvcreate”如何解决 - 编程语言

今天小编给大家分享一下php报错“ocienvcreate”如何解决的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。 一、什么...