问题描述
我有一个像这样的字符串: “启动字符串{替换此字符串}继续字符串{也替换此字符串},并在适当的情况下,再继续添加字符串” 我需要的结果是: “开始字符串%,继续字符串%,并在适当的情况下,再继续字符串'
我正在尝试使用%替换所有出现的开/闭波浪状括号,以便在使用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'