如何从字符的第 4 次出现到 PSQL 中给定字符串的结尾获取子字符串 [英] How to get substring from 4th occurence of a character until the end of given string in PSQL

查看:47
本文介绍了如何从字符的第 4 次出现到 PSQL 中给定字符串的结尾获取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例

我有一个字符串...

'/this/is/a/given/string/test.file'.

'/this/is/a/given/string/test.file'.

如何在 PSQL 中获取子字符串 'given/string/test.file'?

How can I get substring 'given/string/test.file' in PSQL?

谢谢!

推荐答案

可以使用正则表达式

with example(str) as (
    values('/this/is/a/given/string/test.file')
)

select regexp_replace(str, '(/.*?){4}', '')
from example;

     regexp_replace     
------------------------
 given/string/test.file
(1 row) 

或函数string_to_array():

select string_agg(word, '/' order by ord)
from example,
unnest(string_to_array(str, '/')) with ordinality as u(word, ord)
where ord > 4;

另请阅读如何在一行中找到第 3 次出现的模式.

这篇关于如何从字符的第 4 次出现到 PSQL 中给定字符串的结尾获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆