尝试:
SELECT substr(str, instr(str, '/', -1) + 1)
FROM (
SELECT '/Name of Folder I want/Another folder...../Folder Im looking from' AS str FROM dual
)
;
instr的“-1”参数告诉 Oracle 从末尾开始返回第一次出现的“/”字符。
编辑:对不起,我读错了,另一个建议(丑陋的):
SELECT substr(str, 2, CASE
WHEN instr(str, '/', 2, 1) > 0 THEN instr(str, '/', 2, 1) - 2
ELSE length(str) - 1
END)
FROM (
SELECT '/Name of Folder I want/Another folder...../Folder Im looking from' AS str FROM dual
UNION ALL
SELECT '/Name of Folder I want' AS str FROM dual
)
;
正则表达式提供了另一种可能的解决方案,尽管我们正在处理的字符串必须将“/”字符连接到末尾:
SELECT regexp_substr(str || '/', '/(.*?)/', 1, 1, NULL, 1)
FROM (
SELECT '/Name of Folder I want/Another folder...../Folder Im looking from' AS str FROM dual
UNION ALL
SELECT '/Name of Folder I want' AS str FROM dual
)
;
这里我们利用了子表达式 - *regexp_substr* 的最后一个参数告诉 Oracle 只返回第一对括号内的字符串部分。