您几乎可以REGEXP_REPLACE()
通过反向参考到达那里:
REGEXP_REPLACE(REMARKS,'.*(PT LIAB|LIAB|LIABLE) (-?\d+[.]?\d+).*', '\2')
...但是通过 untouched 传递一个没有匹配模式的值(所以你的第三个例子仍然会得到*SOC = 1178.00
. 你可以使用 case 表达式并REGEXP_LIKE()
避免这种情况:
with t (remarks) as (
select '*SOC 1369.00 - NCS 1239.46 = PT LIAB 129.54' from dual
union all select '*SOC 1369.00 - NCS 1239.46 = PT LIAB 140' from dual
union all select '*SOC = 1178.00' from dual
union all select '*SOC 1622.00 - NCS 209.74 = PT LIAB 1412.26 RECIPIENT AGE' from dual
union all select '*LINE #1 SOC 0.00 - NCS 22.77 = LIAB -22.77' from dual
union all select 'SOC MET AND CLEARED, SOC 2062-NCS 498.56=PT LIABLE 1563.44' from dual
)
SELECT REMARKS,
CASE WHEN REGEXP_LIKE(REMARKS, '.*(PT LIAB|LIAB|LIABLE) (-?\d+[.]?\d+).*')
THEN REGEXP_REPLACE(REMARKS,'.*(PT LIAB|LIAB|LIABLE) (-?\d+([.]\d+)?).*', '\2')
END as liability
from t;
REMARKS LIABILITY
---------------------------------------------------------- ----------
*SOC 1369.00 - NCS 1239.46 = PT LIAB 129.54 129.54
*SOC 1369.00 - NCS 1239.46 = PT LIAB 140 140
*SOC = 1178.00
*SOC 1622.00 - NCS 209.74 = PT LIAB 1412.26 RECIPIENT AGE 1412.26
*LINE #1 SOC 0.00 - NCS 22.77 = LIAB -22.77 -22.77
SOC MET AND CLEARED, SOC 2062-NCS 498.56=PT LIABLE 1563.44 1563.44
但这似乎并没有好多少,并且两次使用正则表达式使其更加昂贵。(它可能还可以简化......)。您也可以使用REGEXP_REPLACE()
而不是两个普通REPLACE()
调用:
REGEXP_REPLACE(
REGEXP_SUBSTR(REMARKS, '(PT LIAB|LIAB|LIABLE) (-?\d+([.]\d+)?)'),
'(PT LIAB|LIAB|LIABLE) ')
但这又使它变得更加昂贵。