您可以使用regexp_replace 删除所有与您的模式不匹配的内容。
with t (col) as (
select 'My twwet #HashTag1 and this is the #SecondHashtag sample, #onemorehashtag'
from dual
)
select
regexp_replace(col, '(#\S+\s?)|.', '\1')
from t;
生产;
#HashTag1 #SecondHashtag #onemorehashtag
regexp_substr 将返回一个匹配项。你可以做的是使用connect by将你的字符串变成一个表格:
with t (col) as (
select 'My twwet #HashTag1 and this is the #SecondHashtag sample, #onemorehashtag'
from dual
)
select
regexp_substr(col, '#\S+', 1, level)
from t
connect by regexp_substr(col, '#\S+', 1, level) is not null;
返回:
#HashTag1
#SecondHashtag
#onemorehashtag
编辑:
\S 匹配任何非空格字符。最好使用匹配 a-z、A-Z、0-9 和 _ 的 \w。
正如@mathguy 和this 网站所评论的那样:
主题标签以字母开头,然后允许使用字母数字字符或下划线。
所以,#[[:alpha:]]\w* 模式会更好。
with t (col) as (
select 'My twwet #HashTag1, this is the #SecondHashtag. #onemorehashtag'
from dual
)
select
regexp_substr(col, '#[[:alpha:]]\w*', 1, level)
from t
connect by regexp_substr(col, '#[[:alpha:]]\w*', 1, level) is not null;
生产:
#HashTag1
#SecondHashtag
#onemorehashtag