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'
 

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...