我需要替换所有没有空白区域的地方,其中第一个单词以点结尾,其中两个单词以空格分隔。
例如,我有'num.some'
之类的字符串,我需要'num. some'
但如果我有'num. some'
,我就不需要'num. some'
(< -this有2个空格)
如果我有'123.4'
,我也不想要'123. 4'
如果我有'123.some'
,我需要'123. some'
我尝试了不同的regexp组合,但我的答案总是出错。
答案 0 :(得分: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
这会在一个字母后跟一个没有空格的字母后面找一个点
答案 1 :(得分: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
答案 2 :(得分:0)
也许你不需要正则表达式。普通replace()
可能对您有用。
(关于@mathguy的测试数据)
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
;