【问题标题】:Replace hostname regex替换主机名正则表达式
【发布时间】:2018-02-07 17:16:49
【问题描述】:

我需要提取主机名并将其替换为 {HOST_NAME} 我用这个表达方式

SELECT regexp_replace(str, '[\/]{2}(.*)[\.|:]', '{HOST_NAME}', 1) from dual
str = http://localhost:8080/test
str = https://test-app.uk.ru/test

预期结果:

> http://localhost:8080/test -> http://{HOST_NAME}:8080/test
> https://test-app.uk.ru/test - > https://{HOST_NAME}.uk.ru/test

结果:

http://localhost:8080/test -> http:{HOST_NAME}8080/test
https://test-app.uk.ru/test - > https:{HOST_NAME}uk.ru/test

【问题讨论】:

  • 您使用的是哪个 dbms?
  • SELECT regexp_replace(str, '(?

标签: sql regex hostname


【解决方案1】:

您可以使用以下方法:

SELECT regexp_replace(str, '://[^.:]+', '://{HOST_NAME}', 1) from dual

请参阅regex demo

://[^.:]+ 匹配:

  • :// - 文字子串
  • [^.:]+ - 除.: 之外的1 个或多个字符

查看online demo

【讨论】:

    【解决方案2】:

    (?<=[\/]{2})([a-z-]{1,})(?=[\.|:])

    Demo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 2015-01-24
      • 1970-01-01
      相关资源
      最近更新 更多