匹配可选字符串前的内容
Match content before optional string
给定以下字符串,我想生成:A-2010
:
/Space/w_00123/A-2010/u_23
/Space/w_00123/A-2010
(上面的 /u_23 是可选的,所以这里没有)
所以 3 和 4/之间的文本(如果存在,或者直到字符串结尾)是我真正需要的。
我试过了:
select
regexp_substr('/Space/w_00123/A-2010/u_23', '/Space/w_.+/(.*?)(?:/.+|$)', 1, 1, null, 1) r
from dual; -- this results in u_23 as opposed to A-2010
此处正确的匹配器表达式是什么?
像这样使用取反 类:
select
regexp_substr('/Space/w_00123/A-2010/u_23', '/Space/w_[^/]+/([^/]+)', 1, 1, null, 1) r
from dual
使用第 3 次出现的 regexp_substr
作为第 4 个参数可以满足您的需求
with t(str) as
(
select '/Space/w_00123/A-2010/u_23' from dual union all
select '/Space/w_00123/A-2010' from dual
)
select regexp_substr(str,'([^/]+)',1,3) as "Result Strings"
from t;
Result Strings
--------------
A-2010
A-2010
给定以下字符串,我想生成:A-2010
:
/Space/w_00123/A-2010/u_23
/Space/w_00123/A-2010
(上面的 /u_23 是可选的,所以这里没有)
所以 3 和 4/之间的文本(如果存在,或者直到字符串结尾)是我真正需要的。
我试过了:
select
regexp_substr('/Space/w_00123/A-2010/u_23', '/Space/w_.+/(.*?)(?:/.+|$)', 1, 1, null, 1) r
from dual; -- this results in u_23 as opposed to A-2010
此处正确的匹配器表达式是什么?
像这样使用取反 类:
select
regexp_substr('/Space/w_00123/A-2010/u_23', '/Space/w_[^/]+/([^/]+)', 1, 1, null, 1) r
from dual
使用第 3 次出现的 regexp_substr
作为第 4 个参数可以满足您的需求
with t(str) as
(
select '/Space/w_00123/A-2010/u_23' from dual union all
select '/Space/w_00123/A-2010' from dual
)
select regexp_substr(str,'([^/]+)',1,3) as "Result Strings"
from t;
Result Strings
--------------
A-2010
A-2010