Redshift SQL 可以执行不区分大小写的正则表达式求值吗?

Posted

技术标签:

【中文标题】Redshift SQL 可以执行不区分大小写的正则表达式求值吗?【英文标题】:Can Redshift SQL perform a case insensitive regular expression evaluation? 【发布时间】:2016-07-26 17:50:37 【问题描述】:

文档说 regexp_instr() 和 ~ 是区分大小写的 Posix 评估函数和运算符。 是否有不区分大小写的 Posix 语法,或基于 PCRE 的函数或运算符的插件

在 Redshift 查询中尝试的 PCRE 示例由于 POSIX'ness 而无法正常工作。

select 
  A.target
, B.pattern
, regexp_instr(A.target, B.pattern) as rx_instr_position
, A.target ~ B.pattern as tilde_operator
, regexp_instr(A.target
, 'm/'||B.pattern||'/i') as rx_instr_position_icase
from
(      select 'AbCdEfffghi' as target 
 union select 'Chocolate' as target 
 union select 'Cocoa Latte' as target 
 union select 'coca puffs, delivered late' as target
) A
,
(      select 'choc.*late' as pattern 
 union select 'coca.*late' as pattern 
 union select 'choc\w+late' as pattern
 union select 'choc\\w+late' as pattern
) B

【问题讨论】:

您想在 POSIX 正则表达式风格中使用什么模式? 【参考方案1】:

回答您的问题:据我所知,没有与 Redshift 兼容的语法或插件。如果您可以使用解决方法:我们最终在字符串周围使用lower() 进行匹配:

select
  A.target
, B.pattern
, regexp_instr(A.target, B.pattern) as rx_instr_position
, A.target ~ B.pattern as tilde_operator
, regexp_instr(A.target, 'm/'||B.pattern||'/i') as rx_instr_position_icase
, regexp_instr(lower(A.target), B.pattern) as rx_instr_position_icase_by_lower
from
(      select 'AbCdEfffghi' as target
 union select 'Chocolate' as target
 union select 'Cocoa Latte' as target
 union select 'coca puffs, delivered late' as target
) A
,
(      select 'choc.*late' as pattern 
 union select 'coca.*late' as pattern 
 union select 'choc\w+late' as pattern
 union select 'choc\\w+late' as pattern
) B

【讨论】:

【参考方案2】:

select 'HELLO' ~* 'el' = 真

目前没有记录 (2020-11-05)

【讨论】:

【参考方案3】:

Redshift 现在通过添加的函数参数为不区分大小写的正则表达式标志提供直接解决方案:Amazon Redshift - REGEXP_INSTR

使用提供的查询示例的语法是:

select
  A.target
, B.pattern
, regexp_instr(A.target, B.pattern) as rx_instr_position
, A.target ~ B.pattern as tilde_operator
, regexp_instr(A.target, B.pattern, 1, 1, 0, 'i') AS rx_instr_position_icase
from
(      select 'AbCdEfffghi' as target
 union select 'Chocolate' as target
 union select 'Cocoa Latte' as target
 union select 'coca puffs, delivered late' as target
) A
,
(      select 'choc.*late' as pattern 
 union select 'coca.*late' as pattern 
 union select 'choc\w+late' as pattern
 union select 'choc\\w+late' as pattern
) B

【讨论】:

以上是关于Redshift SQL 可以执行不区分大小写的正则表达式求值吗?的主要内容,如果未能解决你的问题,请参考以下文章

oracle的sql命令有没有区分大小写

SQL语言必须大写吗?

如何在 SQL 2000/2005 中执行区分大小写的搜索和替换?

SQL不是不区分大小写吗

sql网络服务的编辑器怎么调出来

Oracle查询语句区分大小写吗?