SQL ( /); " "; null , null , ( , ).
, "", - , . OP.
. , , id = 156, , "" , , - "" . - .
, ( ), , , ( ) , . , " " ) (.
: , "" (-regexp) . .
Query
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, name, instr(name, ')', -1)
from species_str
),
rec ( id, name, str, len, prev_pos, new_pos, flag ) as (
select id, name, substr(name, 1, instr(name, ')', -1)),
pos, pos - 1, pos, null
from prep
union all
select id, name, str, len, new_pos,
instr(str, '(', -(len - new_pos + 2)),
case when length(replace(substr(str, new_pos), '(', '')) =
length(replace(substr(str, new_pos), ')', ''))
then 1 end
from rec
where prev_pos > 0 and flag is null
)
select id, name, case when flag = 1
then substr(name, prev_pos, len - prev_pos + 1) end as target
from rec
where flag = 1 or prev_pos <= 0 or name is null
order 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 ( ), case when flag = 1 then... target, :
... , 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