在POSTGRES

时间:2015-04-24 23:11:31

标签: regex postgresql url-pattern greenplum

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

上面的代码给了我

  

您好

而不是

  

蟋蟀/你好

我检查了Regexp检查网站并且模式是正确的。 我不知道我哪里出错了。

  

DBMS:“PostgreSQL 8.2.15(Greenplum Database 4.2.8.3 build 1)on   x86_64-unknown-linux-gnu,由GCC gcc(GCC)4.4.2汇编编译而成   2014年11月2日01:33:14“

3 个答案:

答案 0 :(得分:0)

试试这个:

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

也可以使用cctld:

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

答案 1 :(得分:0)

我假设你想要一个URL的路径部分。

我没有我的pg,但我会非常清楚地知道网址的每一部分 -

'[^:]+:\/\/[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

答案 2 :(得分: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

enter image description here