没有白色的 Oracle 正则表达式 space

Oracle regexp without white space

我需要替换所有没有白色 space beatween 2 个单词的地方,其中第一个以点结尾,这两个单词带有白色 space beatween。

例如,我有 'num.some' 这样的字符串,我需要 'num. some'

但是如果我有'num. some',我就不需要'num. some'(<-这个有2个白色space)

如果我有'123.4',我也不想要'123. 4' 如果我有 '123.some',我需要 '123. some'

我尝试了不同的正则表达式组合,但我的答案总是有问题。

类似的内容可能会对您有所帮助:

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]+)','. ') replaced
FROM examples

这会在一个字母后跟一个不带空格的字母后查找一个点 space

这会查找所有组合(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:]]))',
          ' ') 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

也许您不需要正则表达式。普通 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
;