Oracle数据库子字符串::从URL提取IP

时间:2018-10-18 20:25:04

标签: sql oracle

我有一个可以通过select语句访问的字段,该字段具有:

https://<some IPv4 address>:someport/xxxxxxx/yyyyyyy

其中xxxxxxxyyyyyyy可能有所不同。

如何修剪左右不需要的数据以获取IP?

该元素也可能看起来像一个jdbc字符串。

谢谢!

2 个答案:

答案 0 :(得分:0)

好的旧SUBSTR + INSTR可能会有所帮助-参加//与以下/:之间(如果存在的话)

SQL> with test (url) as
  2    (select 'https://stackoverflow.com/questions/52881996/' from dual union all
  3     select 'https://localhost:8008/whatever' from dual)
  4  select
  5    substr(url,
  6           instr(url, '//') + 2,
  7           case when instr(url, ':', 1, 2) = 0 then instr(url, '/', 1, 3)
  8                else instr(url, ':', 1, 2)
  9           end - instr(url, '//') - 2
 10          ) result
 11  from test;

RESULT
--------------------------------------------------------------------------------
stackoverflow.com
localhost

SQL>

答案 1 :(得分:0)

使用REGEXP_SUBSTR

的其他方法
select
  REGEXP_SUBSTR(
    REGEXP_SUBSTR('https://:someport/xxxxxxx/yyyyyyy','[^:]+',1,3),
    '[^/]+',1,1
  )
from dual;

结果:某端口

select
  REGEXP_SUBSTR(
    REGEXP_SUBSTR('https://<some IPv4 address>:someport/xxxxxxx/yyyyyyy','[^//]+',1,2),
    '[^:]+',1,1
  )
from dual;

结果:一些IPv4地址

相关问题