首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将字符串解析为片段的Regexp_substr

将字符串解析为片段的Regexp_substr
EN

Stack Overflow用户
提问于 2014-02-25 19:00:02
回答 2查看 244关注 0票数 1

我整个上午都被困在这件事上,希望能得到一些帮助。我一直在阅读我能找到的东西,但我在把它应用于我的情况时遇到了困难。

我有类似的记录:

代码语言:javascript
复制
A123-700
A123-700 / WORD-8
A123 / A456
WORD-8 / A456-800

我需要把它们分解成“类型”和“系列”,忽略"WORD-8“。

例如

代码语言:javascript
复制
A123-300 would be type=A123, series=300
A123-300 / WORD-8 would be type=A123, series=300
A123 / A456 would be type=A123, type=A456
WORD-8 / A456-200 would be type=A456, series=200

到目前为止,我有这样的事情:

代码语言:javascript
复制
WITH gen AS
  ( select 'A123-700' x from dual
  UNION ALL
  select 'A123-700 / WORD-8' x from dual
  union all
  select 'A123 / A456' x from dual
  union all
  select 'WORD-8 / A456-800' x from dual
  )
SELECT x ,
  regexp_substr(x, '[^/]+')              as first_slash,
  regexp_substr(x, '[^-]+')              as first_type,
  regexp_substr(x, '-\w*')               as first_series,
  regexp_substr(x, '[^/][^DASH]+', 1, 2) as second_slash,
  regexp_substr(x, '[^/]+', 1, 2)        as second_type,
  regexp_substr(x, '-\w+', 1, 2)         as second_series
FROM gen;

但结果不是我所期望的。我不想有-,我的“第二”信息也不太好。

代码语言:javascript
复制
X                 FIRST_SLASH FIRST_TYPE  FIRST_SERIES  SECOND_SLASH  SECOND_TYPE SECOND_SERIES
A123-700          A123-700    A123        -700          (null)        (null)      (null)
A123-700 / WORD-8 A123-700    A123        -700          D-8           WORD-8      -8
A123 / A456       A123        A123 / A456 (null)        A456          A456        (null)
WORD-8 / A456-800 WORD-8      WORD        -8            D-8 /         A456-800    -800

有人能帮我指出正确的方向吗?

谢谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-25 19:31:34

代码语言:javascript
复制
WITH 
  gen AS ( 
    select 'A123-700' x from dual
    UNION ALL
    select 'A123-700 / WORD-8' x from dual
    union all
    select 'A123 / A456' x from dual
    union all
    select 'WORD-8 / A456-800' x from dual
  ),
  t_slash as (
    SELECT x ,
      nullif(regexp_replace(x, '\s*/.*$'),'WORD-8') as first_slash,
      nullif(regexp_replace(x, '^[^/]*/?\s*'),'WORD-8') as second_slash
    FROM gen
  )
select x, first_slash, 
  regexp_substr(first_slash, '^[^-]*') as first_type,
  regexp_replace(first_slash, '^[^-]*-?') as first_series,
  second_slash,
  regexp_substr(second_slash, '^[^-]*') as second_type,
  regexp_replace(second_slash, '^[^-]*-?') as second_series
from t_slash

小提琴

票数 1
EN

Stack Overflow用户

发布于 2014-02-25 19:32:31

代码语言:javascript
复制
WITH gen AS
  ( select 'A123-700' x from dual
  UNION ALL
  select 'A123-700 / WORD-8' x from dual
  union all
  select 'A123 / A456' x from dual
  union all
  select 'WORD-8 / A456-800' x from dual
  )
SELECT x ,
  regexp_substr(x,'A[[:alnum:]]+',1) as first_type,
  NULLIF( regexp_substr(x,'A[[:alnum:]]+',2),
          regexp_substr(x,'A[[:alnum:]]+',1))  as second_type,
  regexp_substr(x,'(A[[:alnum:]]+)-([[:digit:]]+)',1) as full,
  regexp_substr(regexp_substr(x,'(A[[:alnum:]]+)-([[:digit:]]+)',1),
                '-([[:digit:]]+)',
                1) as first_series
FROM gen;
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22023670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档