2017-04-05 92 views
0

我正在处理红移&想写一些查询,它将删除第一次出现的括号内的消息。在红移有正则表达式查找第一次出现

输入: -

FR_3000 Error opening file [File_Location]. Operating system error message [The system cannot find the path specified.]. 

输出: -

FR_3000 Error opening file []. Operating system error message [The system cannot find the path specified.]. 

我想下面的查询,但未能解决问题..

select regexp_replace(description,'\[(.*?)\]','') from emp; 
+0

告诉我们它不能解决你的问题。这可能是错误的正则表达式,甚至错误的表或列名称 –

回答

0

不知道是什么你的问题是。我没有任何环境可以在Redshift中进行测试。我猜根据你的标签,那个替换太贪心了,结果变成FR_3000 Error opening file [].

一种方法是用

\1[]\2 

说明更换

^(.*?)\[.*?\](.*)$ 

^     Start of line 
( )    group 1 
    .*?    any number of character, reluctant match 
        (match at least as possible) 
     \[   follow by open square bracket 
     .*?   any number of char, reluctant match 
      \]  follow by closing square bracket 
      (.*) group 2, of any number of character 
       $ till the end 

根据红移请调整的语法。

万一红移甚至不支持勉强量词,改变正则表达式是这样的:

^([^\[]*)\[[^\]]*\](.*)$ 

测试正则表达式中https://regex101.com/r/IDEvK3/1

1

红移正规快件功能没有组的任何概念捕获,所以解决方案将不具有纯正则表达式的纯度。

只要你知道,“]”初审会经常来的第一个实例后,“[”,你可以使用:

select left(description, charindex('[', description)) || substring(description, charindex(']', description)) from emp; 

如果有可能,你有一个流浪“]”在您的字符串的开头,你可以使用略少效率:

select left(description, charindex('[', description)) || substring(description, REGEXP_INSTR(description, '\]', charindex('[', description))) from emp; 

我们正在做的这些声明有何第一左括号,这是charindex('[', description)位置,一切都在第一右括号前后正在采取一切,位于charindex(']', description),然后将它们与||运算符连接起来。

+0

谢谢杰森!它有帮助 –

相关问题