如何提取最后一个和倒数第二个空格之间的所有字符?

发布于 01-09 22:05 字数 382 浏览 1 评论 0原文

例如,我有以下列:

col1
A BCDE
ABCE
ABC

我想得到以下内容:

col1
D
C
B

谢谢!

I have for example the following column:

col1
A B C D E
A B C E
A B C

I want to get the following:

col1
D
C
B

Thanks !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈独秀2025-01-16 22:05:37

您可以使用 regexp_extract:

与 mytable as (
选择 'ABCD E' col1 联合所有
选择“ABC E”并全部
选择“AB C”
)

select regexp_extract(col1, '\s+([^\s]+)\s+[^\s]*$',1) from mytable

结果:

D
    
C
    
B

Regex '\\s+([^\\s]+ )\\s+[^\\s]*$' 表示:

\\s+ - 空格 1+ 倍

([^\\s]+) - 要提取的组,而不是空格1+ 次

\\s+ - 1+ 空格

[^\\s]* - 任何时候都不是空格

在此处输入代码$ - 结束字符串的

You can use regexp_extract:

with mytable as (
select 'A B C D E' col1 union all
select 'A B C E' union all
select 'A B C'
)

select regexp_extract(col1, '\s+([^\s]+)\s+[^\s]*$',1) from mytable

Result:

D
    
C
    
B

Regex '\\s+([^\\s]+)\\s+[^\\s]*$' means:

\\s+ - space 1+ times

([^\\s]+) - group to extract, not space 1+ times

\\s+ - 1+ space

[^\\s]* - not a space any times

enter code here$ - end of the string

梨涡2025-01-16 22:05:37

您可以使用下面的代码。

select split('A B C D E',' ')[ length('A B C D E')- length(replace('A B C D E',' ','') )-1 ] col 

split - 这将根据空格选择特定的字符串。
length - 智能地使用它来计算最后一个空格之前的最后一个字符串。
length (整个字符串) - length (整个字符串,不含空格) - 这应该给你空格数。执行 -1 以获得最后一个空格计数。

You can use below code.

select split('A B C D E',' ')[ length('A B C D E')- length(replace('A B C D E',' ','') )-1 ] col 

split - this is going to choose particular string based on spaces.
length - This is used intelligently to calculate last string before last space.
length (whole string ) - length (whole string without spaces) - This should give you number of spaces. Do a -1 to get last but one space count.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文