2014-02-25 4 views
1

나는 이것에 아침 내내 고착되어 도움을 얻기를 바라고 있습니다. 나는 내가 찾을 수있는 것을 읽고 있었지만 나의 상황에서는 그것을 적용하는 데 어려움을 겪고있다.문자열을 조각으로 구문 분석하는 Regexp_substr

나는이 비슷한 기록을 가지고

A123-700 
A123-700/WORD-8 
A123/A456 
WORD-8/A456-800 

나는 "WORD-8"

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 
예를

를 들어 "유형"과 "시리즈"로 다음을 깰 무시해야

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; 
,369 :

지금까지이 같은 뭔가를

그러나 결과는 내가 원하는 것이 아닙니다. 저는 - 및 제 "두 번째"정보를 갖고 싶지 않습니다.

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 

누군가 나를 올바른 방향으로 안내 할 수 있습니까?

감사합니다.

답변

1
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 

fiddle

+0

정말 도움 즉, 감사합니다! 한 가지 질문입니다. 'WORD-8'또는 'WORD 8'일 수 있다면 어떨까요? – froglander

+0

@froglander - WORD-8 대신에 임의의 문자열을 사용할 수 있습니다. –

1
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; 
관련 문제