在postgresql中使用正则表达式时需要使用关键字“~
”,以表示该关键字之前的内容需匹配之后的正则表达式,若匹配规则不需要区分大小写,可以使用组合关键字“~*
”;
相反,若需要查询不匹配这则表达式的记录,只需在该关键字前加否定关键字“!
”即可。若正则表达式包含转义字符,则需在表达式前加关键字“E
”。
Operator | Description | Example |
---|---|---|
~ | Matches regular exPRession, case sensitive | 'thomas' ~ '.*thomas.*' |
~* | Matches regular expression, case insensitive | 'thomas' ~* '.*Thomas.*' |
!~ | Does not match regular expression, case sensitive | 'thomas' !~ '.*Thomas.*' |
!~* | Does not match regular expression, case insensitive | 'thomas' !~* '.*vadim.*' |
例如:
select * from user where email ~ '^[A-H]' --匹配email地址以A-H开头的记录 select * from user where email ~* '^[a-h]' --匹配email地址以A-H和a-h开头的记录正则表达式的匹配公式是通用的,可以参照正则表达式(regular expression)以及常用语法
其他关于PostgreSQL的正则表达式的用法和解释可以参考:PostgreSQL 8.1 中文文档
可以看一下阿里云数据库ApsaraDB上的一篇文章:PostgreSQL 百亿数据 秒级响应 正则及模糊查询
参考资料: 网上资料 备注: 转载请注明出处:http://blog.csdn.net/wsyw126/article/details/59153228 作者:WSYW126
新闻热点
疑难解答