1

我需要在两个单词之间替换所有没有空格的地方,其中第一个以点结尾,这两个单词在它们之间带有空格。

例如,我有类似的字符串'num.some',我需要'num. some'

但如果我有'num. some',我不需要'num. some'(<-这有 2 个空格)

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

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

4

3 回答 3

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

这会在一个字母之后寻找一个点,然后是一个没有空格的字母

于 2016-10-27T09:45:13.923 回答
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
;
于 2016-10-27T11:59:42.023 回答
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
于 2016-10-27T10:43:54.863 回答