【问题标题】:Oracle REGEXP_SUBSTR Split URLOracle REGEXP_SUBSTR 拆分 URL
【发布时间】:2021-07-14 20:25:16
【问题描述】:

我没有过多使用正则表达式,但是,我想将 URL 分解为 URL 的不同部分:即 SCHEME(http 或 https)、IP_ADRESS(1.1.1.1 或主机名,例如dummy.org)、PORT(它实际上可以是任意长度),最后是 CONTEXT

正如您在下面看到的,这是我迄今为止设法实现的目标,可能不是最好的方法:)。然而,我目前唯一的困难是如果 IP 地址是一个数字,我已经满足了,但如果它是一个主机名,我也想满足。您如何满足第二个/和第一个之间的任何问题:? 最后,我在上下文中也遇到了困难,我似乎无法理解第二部分。我需要从第三个 / 到最后的任何值

*SELECT 
    REGEXP_SUBSTR(url, '^https?*') AS "SCHEME",
    REGEXP_SUBSTR(url, '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+\') AS "IP_ADDRESS",
    REGEXP_REPLACE(REGEXP_SUBSTR(url, '[0-9]+\.*/'),'[/]', '') AS "PORT",
    REGEXP_SUBSTR(url, '^https?://[^/]+(.*/)[^/].*', 1, 1, null, 1) AS "CONTEXT"
from (
     select 'https://1.2.3.4:80801/test/test2' as url from dual
    );*
SCHEME IP_ADDRESS PORT CONTEXT
https 1.2.3.4 80801 /test/

提前感谢您的帮助:)

【问题讨论】:

  • 这不应该简单很多吗?该方案始终以:// 结尾,IP 地址(带有可选端口)在下一个/ 处结束,如果端口存在,它以冒号开头,位于结束方案的:// 和下一个@987654326 之间@?

标签: regex oracle substring


【解决方案1】:

试试这个:

SELECT url,
    REGEXP_SUBSTR(url, '^([^:]*)://',1,1,null,1) AS "SCHEME",
    REGEXP_SUBSTR(url, '^([^:]*://)?([^/:]+)(:|/|$)',1,1,null,2) AS "HOST",
    REGEXP_substr(url, '^([^:]*://)?([^/:]+):(\d+)',1,1,null,3) AS "PORT",
    REGEXP_SUBSTR(url, '^([^:]*://)?([^/]+)(/.*)?', 1, 1, null, 3) AS "CONTEXT"
from (
     select 'https://1.2.3.4:80801/test/test2' as url from dual
     union all select 'https://example.com:80801/test/test2' as url from dual
     union all select 'https://example.com/test/test2' as url from dual
     union all select 'http://example.com:8080/test/test2' as url from dual
     union all select 'http://example.com/' as url from dual
     union all select 'http://example.com' as url from dual
     union all select 'http://example.com:888' as url from dual
     union all select 'http://www.example.com' as url from dual
     union all select 'ftp://example.com/test/test2' as url from dual
     union all select 'skype://username' as url from dual
    );

结果:

URL                                  SCHEME HOST                 PORT     CONTEXT
------------------------------------ ------ -------------------- -------- --------------------
https://1.2.3.4:80801/test/test2     https  1.2.3.4              80801    /test/test2
https://example.com:80801/test/test2 https  example.com          80801    /test/test2
https://example.com/test/test2       https  example.com                   /test/test2
http://example.com:8080/test/test2   http   example.com          8080     /test/test2
http://example.com/                  http   example.com                   /
http://example.com                   http   example.com
http://example.com:888               http   example.com          888
http://www.example.com               http   www.example.com
ftp://example.com/test/test2         ftp    example.com                   /test/test2
skype://username                     skype  username

【讨论】:

    猜你喜欢
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-22
    • 2021-12-06
    • 2022-11-24
    • 2014-05-28
    • 1970-01-01
    相关资源
    最近更新 更多