Warm tip: This article is reproduced from serverfault.com, please click

oracle12c-如何从Oracle的空间中提取单词?

(oracle12c - How to extract words from space in oracle?)

发布于 2020-11-28 03:14:38

我在列文件名中有一个句子,例如:

filename
Exact filename from California Dec 2015
Exact filename from Austin Jan 2015

我只想像我的预期输出那样从中提取日期部分:

filename
Dec 2015
Jan 2015

但是我搜索了Stackoverflow,但发现了两个结果,但它们对我没有用。

select regexp_substr(regexp_replace(filename
                                   ,'[[:space:]]-[[:space:]]'
                                   ,chr(11))
                    ,'([^'||chr(11)||']*)('||chr(11)||'|$)'
                    ,1 -- Start here
                    ,4 -- return 1st, 2nd, 3rd, etc. match
                    ,null
                    ,1 -- return 1st sub exp
                    )

我尝试的另一种方法是:

regexp_like(filename,'(^[:space:]|[:space:]$)');

但是,这两种解决方案都不适用于我。我在SO上进行了更多搜索,但没有得到。

Questioner
Dikshit Karki
Viewed
0
Vasan 2020-11-28 11:47:56

如果你只需要列值中的最后两个“单词”,则可以使用以下命令:

select regexp_substr(filename, '[^[:space:]]+[[:space:]][^[:space:]]+$') from table;

此表达式匹配:(space以外的任何内容)(space)(space以外的任何内容)(end)

请注意,尽管如此,最后两个单词之间仅允许一个空格字符。