2016-10-27 7 views
1

공백없이 2 자리 단어를 모두 교체해야합니다. 첫 번째 단어는 끝으로 끝나고 두 단어는 공백 문자로 구분합니다. ,공백없이 오라클 정규 표현식

예를 들어, 나는 'num.some' 같은 문자열을하고 난 'num. some'

필요하지만 'num. some'이있는 경우, 나는 '123.4'이있는 경우 'num. some'

(< -이 2 공백이) 그리고 필요가 없습니다 또한 '123. 4' 싶지 않아 내가 '123.some'이있는 경우, 나는 정규 표현식의 다른 조합을 시도 '123. some'

필요하지만, 난 항상 있었다 내 대답에 문제가있다. 이 같은

+1

는 지금까지 시도 무엇을 게시하시기 바랍니다 (관련하여 테스트 데이터 @mathguy하는) 당신이 – Aleksej

+0

가있는 문제는 당신은'데이터의 some.123'이 수, 그 필요성을한다 추가 된 공간이 있습니까? – mathguy

답변

2

뭔가 당신을 도울 수 있습니다

WITH examples AS (
    SELECT 'num.some' str FROM dual 
    UNION 
    SELECT 'num. some' str FROM dual 
    UNION 
    SELECT '123.4' str FROM dual 
    UNION 
    SELECT '123.some' str FROM dual 
) 
SELECT str, REGEXP_REPLACE(str,'([a-zA-Z0-9]+)\.([a-zA-Z]+)','\1. \2') replaced 
FROM examples 

을이 빈 공간이이 모든 조합을 찾습니다

+0

두 번째 단어를 두 번 중복 (단어 뒤) 나는 'dol.USA'를 가지고 있었고이 '돌' USA ' 이유를 이해하지 못하겠습니까? –

+0

@AnastasiaSisordia 그것은 'dol.USA'와 함께 나를 위해 일하고있다. – pablomatico

0

(letter.letter, letter.digit없는 문자가 뒤에 편지 후 포인트를 찾습니다 , digit.letter). 뒤에 공백을 추가합니다. digit.digit는 변경되지 않은 상태로 남겨 둡니다.

with 
    inputs (str) as (
     select 'ab.sw' from dual union all 
     select 'ab. sw' from dual union all 
     select '12.33' from dual union all 
     select '12. 33' from dual union all 
     select 'ab.123' from dual union all 
     select '1. ab' from dual union all 
     select '1.abc' from dual 
    ) 
-- END test data. Solution (SQL query) begins below this line. 
select str, regexp_replace(str, 
      '(([[:alpha:]]\.)([[:alpha:]])|([[:alpha:]]\.)(\d)|(\d\.)([[:alpha:]]))', 
      '\2\4\6 \3\5\7') as new_str 
from inputs 
; 


STR  NEW_STR 
------ ------- 
ab.sw ab. sw 
ab. sw ab. sw 
12.33 12.33 
12. 33 12. 33 
ab.123 ab. 123 
1. ab 1. ab 
1.abc 1. abc 
0

어쩌면 정규식이 필요하지 않을 수도 있습니다. 평야 replace()가 도움이 될 수 있습니다.

with 
    inputs (str) as (
     select 'ab.sw' from dual union all 
     select 'ab. sw' from dual union all 
     select '12.33' from dual union all 
     select '12. 33' from dual union all 
     select 'ab.123' from dual union all 
     select '1. ab' from dual union all 
     select '1.abc' from dual 
    ) 
-- END test data. Solution (SQL query) begins below this line. 
select replace(
    replace(str, '.', '. ') -- replace all dots by dot+space 
, ' ' 
, ' ' 
) -- replace all double spaces by a single space 
from inputs 
;