如何解决Redshift regexp_replace
我有一个像这样的字符串: “启动字符串{替换此字符串}继续字符串{也替换此字符串},并在适当的情况下,再继续添加字符串” 我需要的结果是: “开始字符串%,继续字符串%,并在适当的情况下,再继续字符串'
我正在尝试使用%替换所有出现的开/闭波浪状括号,以便在使用like运算符的动态SQL语句中使用。
解决方法
要将{replace this}
之类的字符串替换为%
,可以使用如下正则表达式替换函数:
with input(txt) as (
select 'start the string {replace this} continue the string {replace this too} and for good measure,continue the string some more'::varchar
)
select regexp_replace(txt,'\\{[\\w\\s]*\\}','%') from input;
返回
start the string % continue the string % and for good measure,continue the string some more
但是,除了进行上述描述的两步搜索之外,您首先用%
替换所有模板部分,然后再使用LIKE
,为什么不仅仅进行正则表达式搜索?
with input(txt) as (
select 'start the string {replace this} continue the string {replace this too} and for good measure,continue the string some more'::varchar
)
select txt from input
where
txt ~ 'start the string \\{.*\\} continue the string \\{.*\\} and for good measure,continue the string some more'
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。