regexp_replace:如果尚未存在,则在字符串中插入空格

时间:2014-04-22 15:16:53

标签: sql regex oracle

我有一个包含城镇名称的Oracle表。有些名字以st开头。 (圣)后跟一个空格,例如st. ulrich,有些没有st.paul。 我想在缺少点的位置后添加空格。 我还想删除多个空格。

这可以用regexp_replace吗?

1 个答案:

答案 0 :(得分:0)

以下是使用REGEXP_REPLACE函数的可能解决方案:

-- Match the string "st." followed by zero or more spaces and a word character,
-- replace it with "st." followed by exactly one space and the captured character
select city,
       regexp_replace(city, 'st\.\s*(\w)', 'st. \1' ) as city_formatted
  from t
 order by city;

输出:

CITY               CITY_FORMATTED      
------------------ --------------------
st.   triple space st. triple space    
st.  double space  st. double space    
st. ulrich         st. ulrich          
st.paul            st. paul