【发布时间】:2019-09-02 09:35:28
【问题描述】:
我尝试使用 oracle SQL regexp_replace 过滤 URL 列表中的域名。问题似乎是其中一些确实有端口,而另一些则没有。
从以下示例中,the-super.hosting.com 应替换为 HOSTNAME(但不要在正则表达式中硬编码,因为可能有任何内容) p>
WITH strings AS (
SELECT 'http://wwww11.the-super.hosting.com:9999/aPath/servlet?config=abcLoginNr=%1' s FROM dual union all
SELECT 'http://wwww22.the-super.hosting.com:6666/aPath/servlet?config=abcLoginNr=%2' s FROM dual union all
SELECT 'http://wwww22.the-super.hosting.com:6666/aPath/servlet?config=abcLoginNr=%2' s FROM dual union all
SELECT 'http://wwww04.the-super.hosting.com/aPath/servlet?config#here' s FROM dual
)
SELECT regexp_replace(s,'([[:alpha:]]+://[[:alpha:]]{4}[[:digit:]]{2}\.)(.+)(:9999/|:6666/|/?)(.+)', '\1HOSTNAME\3\4') "MODIFIED_STRING", s "STRING"
FROM strings;
它似乎无法使用普通路径将端口作为可选处理(因为路径直接从那里开始)。
是否可以以不同的方式匹配域部分,以便始终留下带有可选端口的路径?
有没有办法用一个语句替换它?
【问题讨论】:
-
我认为首先
(.+)应该是([^/]+?)。