[英]Get substring between 2 characters in PostgreSQL
I am trying to get the characters between a URL like so in postgreSQL: 我试图像在postgreSQL中那样获取URL之间的字符:
www.abc.com/hello/xyz
www.abc.com/hello/xyz
www.abc.com/hi/pqr
www.abc.com/hi/pqr
www.abc.com/yellow/xyz
www.abc.com/yellow/xyz
I want to get 我想得到
hello
你好
hi
嗨
yellow
黄色
This is what I have so far: 这是我到目前为止的内容:
select distinct substring(url, position('/' in url)+ 1) theURL from table;
I am only able to get the first "/" 我只能得到第一个“ /”
I am not sure how to get the position of the second one 我不确定如何获得第二个职位
One method uses regexp_split_to_array()
: 一种方法使用
regexp_split_to_array()
:
select (regexp_split_to_array(url, '/'::text))[2]
or better yet as @NeilMcGuigan suggests: 或更好,如@NeilMcGuigan建议:
select split_part(url, '/', 2)
Following your substring approach, and using the first substring result to feed a second search: 按照您的子字符串方法,并使用第一个子字符串结果来进行第二次搜索:
select distinct substring(
substring(url, position('/' in url)+ 1)
, 0
, position('/' in substring(url, position('/' in url)+ 1))) AS theURL
from table;
Essentially what the query does is use your original result from substring
to launch a search for the next \\ , so then it is able to keep the text between the first two \\ 本质上,查询所做的是使用
substring
的原始结果启动对下一个\\的搜索,因此它可以将文本保留在前两个\\之间。
And if having them sorted alphabetically is important, you could add an outer query: 并且如果按字母顺序对它们进行排序很重要,则可以添加一个外部查询:
SELECT theURL FROM (
select distinct substring(
substring(url, position('/' in url)+ 1)
, 0
, position('/' in substring(url, position('/' in url)+ 1))) AS theURL
from table
) AS xt
ORDER BY xt.theURL;
Following query will work even for inputs like www.abc.com/hello
以下查询甚至适用于
www.abc.com/hello
输入
SELECT DISTINCT (regexp_matches(url, '/([^/]+)'))[1] theURL
FROM table;
And also it will skip empty entries 而且它将跳过空条目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.