正则表达式找到第一次出现

时间:2017-04-05 01:28:05

标签: regex amazon-redshift regex-greedy

我正在研究redshift&想要编写一些查询,它将仅删除第一次出现的括号内的消息。在红移那里

输入: -

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;

2 个答案:

答案 0 :(得分:1)

Redshift常规表达功能没有任何组捕获的概念,因此解决方案不具备纯正的正则表达式的纯度。

只要您知道']'的第一个实例将永远在第一个实例之后出现['你可以使用:

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)的位置,并且然后将它们与||运算符连接起来。

答案 1 :(得分:0)

不确定您的问题是什么。我没有专门用Redshift测试它的环境。我想根据你的标签,替换太贪心,结果变成where in

一种方法是替换

select *
 from all_data
 where 
   place_id in (select place_id from selected_place_day_hours)
   and day in (select day from selected_place_day_hours)
   and hour in (select day from selected_place_day_hours)
;

FR_3000 Error opening file [].

解释

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

请根据redshift调整语法。

如果redshift甚至不支持不情愿的量词,请将正则表达式改为:

\1[]\2

https://regex101.com/r/IDEvK3/1

中测试正则表达式
相关问题