基本上,我需要从URL或整个网站名称中删除包含域和子域名的行www.
我的DB表看起来像这样:
+----------+------------------------+
| id | website |
+----------+------------------------+
| 1 | https://www.google.com |
+----------+------------------------+
| 2 | http://www.google.co.in|
+----------+------------------------+
| 3 | www.google.com |
+----------+------------------------+
| 4 | www.google.co.in |
+----------+------------------------+
| 5 | google.com |
+----------+------------------------+
| 6 | google.co.in |
+----------+------------------------+
| 7 | http://google.co.in |
+----------+------------------------+
Run Code Online (Sandbox Code Playgroud)
预期产量:
google.com
google.co.in
google.com
google.co.in
google.com
google.co.in
google.co.in
Run Code Online (Sandbox Code Playgroud)
我的Postgres查询看起来像这样:
select id, substring(website from '.*://([^/]*)') as website_domain from contacts
Run Code Online (Sandbox Code Playgroud)
但上面的查询给出了空白网站.那么,我如何获得所需的输出?
您可以使用
SELECT REGEXP_REPLACE(website, '^(https?://)?(www\.)?', '') from tbl;
Run Code Online (Sandbox Code Playgroud)
请参阅正则表达式演示。
细节
^ - 字符串的开始(https?://)?- 1 或 0 次http://或https://(www\.)? - 1 或 0 次 www.请参阅PostgreSQL 演示:
CREATE TABLE tb1
(website character varying)
;
INSERT INTO tb1
(website)
VALUES
('https://www.google.com'),
('http://www.google.co.in'),
('www.google.com'),
('www.google.co.in'),
('google.com'),
('google.co.in'),
('http://google.co.in')
;
SELECT REGEXP_REPLACE(website, '^(https?://)?(www\.)?', '') from tb1;
Run Code Online (Sandbox Code Playgroud)
结果:
你必须使用"非捕获"匹配?:以应对非"http://"网站
喜欢
select
id,
substring(website from '(?:.*://)?(?:www\.)?([^/]*)')
as website_domain
from contacts
Run Code Online (Sandbox Code Playgroud)
http://sqlfiddle.com/#!17/197fb/14
https://www.postgresql.org/docs/9.3/static/functions-matching.html#POSIX-ATOMS-TABLE
| 归档时间: |
|
| 查看次数: |
1848 次 |
| 最近记录: |