Oracle SQL在括号中获取最后一个字符串(也可能包括括号内)

Oracle SQL在括号中获取最后一个字符串(也可能包括括号内),第1张

概述我正在使用此查询: SELECT strain.id, TRIM(SUBSTR(strain.name, 1, INSTR(strain.name, '[')-1)) AS nameFROM species_strain strain 上面的查询给出了类似以下内容: id name-----------------------------------------------100 我正在使用此查询:

SELECT strain.ID,TRIM(SUBSTR(strain.name,1,INSTR(strain.name,'[')-1)) AS nameFROM specIEs_strain strain

上面的查询给出了类似以下内容:

ID    name-----------------------------------------------100   CfwHE3 (HH3d) Jt1 (CD-1)101   4GSdg-3t 22sfG/J (mdx (fq) KO)102   Yf7mMjfel 7(tm1) (SCID)103   B29fj;jfos x11 (tmos (line x11))104   B29;CD (Atm (line G5))105   Ifkso30 jel-3106   13GupSip (te3x) Blhas/J           --------> I don't want to get (te3x)

我需要一个正则表达式,它会给我最后一组括号的内容(可能或者可能不包括一组或多组括号内) – 这需要在字符串的末尾.如果它在字符串的中间,那么我不想要它.

我想得到的是以下内容:

(CD-1)(mdx (fq) KO)(SCID)(tmos (line x11))(Atm (line G5))

因此,如果我复制并粘贴我的整个查询,我有这个,但这并没有考虑到里面的括号:

SELECT disTINCT REGEXP_SUBSTR(strain.name,'\(.*?\)',REGEXP_COUNT(strain.name,'\(.*?\)')) AS nameFROM (  SELECT strain.ID,'[')-1)) AS name  FROM specIEs_strain strain) strainWHERE INSTR(strain.name,'(',1) > 0

查询以某种方式工作,但如果我在主要的一个内部得到另一组括号,它会中断,我会丢失一些数据.它返回如下内容:

(CD-1)(mdx (fq)          ---------> missing KO)(SCID)(tmos (line x11)   ---------> missing )(Atm (line G5)     ---------> missing )

附加要求

我忘了提到我需要的括号集应该在最后.如果之后还有其他字符,那么我不想要它.我在我的例子中添加了另一行.

解决方法 下面的解决方案使用纯sql(无过程/函数);它适用于任何级别的嵌套括号和“同级”括号;并且每当输入为空时它返回null,或者它不包含任何右括号,或者它包含右括号但最右边的右括号是不平衡的(在最右边的左边没有左括号)右括号,这样对是平衡的).

在最底部,我将显示返回“结果”所需的微调,只有当最右边的括号是输入字符串中的最后一个字符时,否则返回null.这是OP的编辑要求.

我创建了几个输入字符串用于测试.请注意,特别是ID = 156,智能解析器不会“计算”字符串文字中的括号,或者以某种其他方式不是“正常”括号.我的解决方案并没有那么远 – 它将所有括号视为相同.

策略是从最右边的右括号(如果有至少一个)的位置开始,并从那里向左移动,一步一步,只通过左括号(如果有的话)并测试是否括号是平衡的.通过比较“测试字符串”之后的长度(除了之后)与长度之间的比较(删除后),可以轻松完成.

额外:我能够使用“标准”(非正则表达式)字符串函数编写没有正则表达式的解决方案.这应该有助于保持快速.

查询:

with     specIEs_str ( ID,name) as (       select 100,'CfwHE3 (HH3d) Jt1 (CD-1)'         from dual union all       select 101,'4GSdg-3t 22sfG/J (mdx (fq) KO)'   from dual union all       select 102,'Yf7mMjfel 7(tm1) (SCID)'          from dual union all       select 103,'B29fj;jfos x11 (tmos (line x11))' from dual union all       select 104,'B29;CD (Atm (line G5))'           from dual union all       select 105,'Ifkso30 jel-3'                    from dual union all       select 106,'13GupSip (te3x) Blhas/J'          from dual union all       select 151,''                                 from dual union all       select 152,'try (this (and (this))) ok?'      from dual union all       select 153,'try (this (and (this)) ok?)'      from dual union all       select 154,'try (this (and) this (ok))?'      from dual union all       select 155,'try (this (and (this)'            from dual union all       select 156,'right grouPing (includging ")")'  from dual union all       select 157,'try this out ) ( too'             from dual     ),prep ( ID,name,pos ) as (       select ID,instr(name,')',-1)       from   specIEs_str     ),rec ( ID,str,len,prev_pos,new_pos,flag ) as (       select  ID,substr(name,-1)),pos,pos - 1,null         from  prep       union all       select  ID,instr(str,-(len - new_pos + 2)),case when length(replace(substr(str,new_pos),'')) =                         length(replace(substr(str,''))                    then 1 end         from  rec         where prev_pos > 0 and flag is null     )select   ID,case when flag = 1               then substr(name,len - prev_pos + 1) end as targetfrom     recwhere    flag = 1 or prev_pos <= 0 or name is nullorder by ID;

输出:

ID name                             TARGET                         ---------- -------------------------------- --------------------------------       100 CfwHE3 (HH3d) Jt1 (CD-1)         (CD-1)                                 101 4GSdg-3t 22sfG/J (mdx (fq) KO)   (mdx (fq) KO)                          102 Yf7mMjfel 7(tm1) (SCID)          (SCID)                                 103 B29fj;jfos x11 (tmos (line x11)) (tmos (line x11))                      104 B29;CD (Atm (line G5))           (Atm (line G5))                        105 Ifkso30 jel-3                                                           106 13GupSip (te3x) Blhas/J          (te3x)                                 151                                                                         152 try (this (and (this))) ok?      (this (and (this)))                    153 try (this (and (this)) ok?)      (this (and (this)) ok?)                154 try (this (and) this (ok))?      (this (and) this (ok))                 155 try (this (and (this)            (this)                                 156 right grouPing (includging ")")                                         157 try this out ) ( too                                              14 rows selected

需要进行更改以满足OP(编辑)的要求:

在最外面的select(在代码的底部),我们在flag = 1的情况下然后…来定义目标列,添加如下条件:

...,case when flag = 1 and len = length(name) then ...

通过此修改输出:

ID name                             TARGET                         ---------- -------------------------------- --------------------------------       100 CfwHE3 (HH3d) Jt1 (CD-1)         (CD-1)                                 101 4GSdg-3t 22sfG/J (mdx (fq) KO)   (mdx (fq) KO)                          102 Yf7mMjfel 7(tm1) (SCID)          (SCID)                                 103 B29fj;jfos x11 (tmos (line x11)) (tmos (line x11))                      104 B29;CD (Atm (line G5))           (Atm (line G5))                        105 Ifkso30 jel-3                                                           106 13GupSip (te3x) Blhas/J                                                 151                                                                         152 try (this (and (this))) ok?                                             153 try (this (and (this)) ok?)      (this (and (this)) ok?)                154 try (this (and) this (ok))?                                             155 try (this (and (this)            (this)                                 156 right grouPing (includging ")")                                         157 try this out ) ( too                                              14 rows selected
总结

以上是内存溢出为你收集整理的Oracle SQL在括号中获取最后一个字符串(也可能包括括号内)全部内容,希望文章能够帮你解决Oracle SQL在括号中获取最后一个字符串(也可能包括括号内)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/1162229.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存