POSTGRES 上 URL 模式的正则表达式问题

Issue of Regular Expression for URL pattern on POSTGRES

select regexp_replace('https://www.facebook.com/cricket/hello', '.*\..*?\/', '')

上面的代码给了我

hello

而不是

cricket/hello

我在 Regexp 检查网站上检查过,模式是正确的。 我不确定我哪里出错了。

DBMS: "PostgreSQL 8.2.15 (Greenplum Database 4.2.8.3 build 1) on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.4.2 compiled on Nov 2 2014 01:33:14"

试试这个:

select regexp_replace('https://www.facebook.com/cricket/hello', '.*\.[a-z]+\/', '')

也可与 cctld 一起使用:

select regexp_replace('https://www.google.co.uk/cricket/hello', '.*\.[a-z]+\/', '')

我假设您想要 URL 的路径部分。

我没有我的 pg,但我会非常明确地说明 URL -

的每个部分
'[^:]+:\/\/[A-Za-z][-a-zA-Z0-9]*(\.[A-Za-z][-a-zA-Z0-9]*)*/'

一个测试:

select testval, regexp_replace ( testval,  '[^:]+:\/\/[A-Za-z][-a-zA-Z0-9]*(\.[A-Za-z][-a-zA-Z0-9]*)*/',  '')
from (
    select 'https://www.facebook.com/cricket/hello' as testval
  union all
  select 'http://a.b.co.uk/cric.ke.t/hello' as testval
  union all
  select 'ftp://a.b.com.d.e.f/relroot/cricket/hello' as testval  union all
  select 'http://www.google.co.uk/cricket/hello' as testval  
  union all
  select 'http://a.b.co.uk/cricket/hello/this/is/a/little/longer?and&it=has&args' as testval
) vals

http://sqlfiddle.com/#!15/9eecb/857/0

我不知道怎么做,但这行得通

.*?\.[a-z]+\/

接受 Andrew Wolfe 对最奇怪的 URL 类型的查询。

select testval, regexp_replace ( testval,  '.*?\.[a-z]+\/',  '')
from (
    select 'https://www.facebook.com/cricket/hello' as testval
  union all
  select 'http://a.b.co.uk/cric.ke.t/hello' as testval
  union all
  select 'ftp://a.b.com.d.e.f/relroot/cricket/hello' as testval  union all
  select 'http://www.google.co.uk/cricket/hello' as testval  
  union all
  select 'http://a.b.co.uk/cricket/hello/this/is/a/little/longer?and&it=has&args' as testval
) vals