暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

PostgreSQL MySQL 兼容性之 正则匹配 REGEXP - <code>~ ~* !~</code>

digoal 2020-10-31
979

作者

digoal

日期

2020-10-31

标签

PostgreSQL , MySQL


背景

字符串正则匹配用法.

MySQL

https://www.runoob.com/mysql/mysql-regexp.html

实例
了解以上的正则需求后,我们就可以根据自己的需求来编写带有正则表达式的SQL语句。以下我们将列出几个小实例(表名:person_tbl )来加深我们的理解:

查找name字段中以'st'为开头的所有数据:

mysql> SELECT name FROM person_tbl WHERE name REGEXP '^st';

查找name字段中以'ok'为结尾的所有数据:

mysql> SELECT name FROM person_tbl WHERE name REGEXP 'ok$';

查找name字段中包含'mar'字符串的所有数据:

mysql> SELECT name FROM person_tbl WHERE name REGEXP 'mar';

查找name字段中以元音字符开头或以'ok'字符串结尾的所有数据:

mysql> SELECT name FROM person_tbl WHERE name REGEXP '^[aeiou]|ok$';

PostgreSQL

https://www.postgresql.org/docs/13/functions-matching.html

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

```
Operator
Description
Example(s)

text ~ text → boolean
String matches regular expression, case sensitively
'thomas' ~ '.thom.' → t

text ~ text → boolean
String matches regular expression, case insensitively
'thomas' ~
'.Thom.' → t

text !~ text → boolean
String does not match regular expression, case sensitively
'thomas' !~ '.thomas.' → f

text !~ text → boolean
String does not match regular expression, case insensitively
'thomas' !~
'.vadim.' → t
```

PostgreSQL 许愿链接

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

9.9元购买3个月阿里云RDS PostgreSQL实例

PostgreSQL 解决方案集合

德哥 / digoal's github - 公益是一辈子的事.

digoal's wechat

文章转载自digoal,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论