regexp_replace:在字符串中插入空格(如果尚不存在) [英] regexp_replace: insert a space in a string if not already present
本文介绍了regexp_replace:在字符串中插入空格(如果尚不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含城镇名称的Oracle表.一些名称以st.开头. (圣)后跟一个空格,例如st. ulrich
,有些不带st.paul
.
我想在缺少点的地方添加空格.
我也想删除多个空格.
I have an Oracle table that contains names of towns. Some names start with st. (saint) followed by a space, e.g. st. ulrich
, some without as st.paul
.
I would like to add the space after the dot where it is missing.
I would also like to strip multiple spaces.
使用regexp_replace
可以吗?
推荐答案
以下是使用REGEXP_REPLACE
函数的可能解决方案:
Here is a possible solution using the REGEXP_REPLACE
function:
-- 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
这篇关于regexp_replace:在字符串中插入空格(如果尚不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文