Regex POSTGRES上URL模式的正则表达式问题
上面的代码给了我 你好 而不是 板球/你好 我在Regexp检查网站上检查过,模式是正确的。 我不知道我哪里做错了 DBMS:“PostgreSQL 8.2.15(Greenplum数据库4.2.8.3构建1)基于 x86_64-unknown-linux-gnu,由GCC(GCC)4.4.2编译,在 2014年11月2日01:33:14“ 试试这个:Regex POSTGRES上URL模式的正则表达式问题,regex,postgresql,url-pattern,greenplum,Regex,Postgresql,Url Pattern,Greenplum,上面的代码给了我 你好 而不是 板球/你好 我在Regexp检查网站上检查过,模式是正确的。 我不知道我哪里做错了 DBMS:“PostgreSQL 8.2.15(Greenplum数据库4.2.8.3构建1)基于 x86_64-unknown-linux-gnu,由GCC(GCC)4.4.2编译,在 2014年11月2日01:33:14“ 试试这个: select regexp_replace('https://www.facebook.com/cricket/hello', '.*\..*?
select regexp_replace('https://www.facebook.com/cricket/hello', '.*\..*?\/', '')
还应与cctld合作:
select regexp_replace('https://www.facebook.com/cricket/hello', '.*\.[a-z]+\/', '')
我假设您需要URL的路径部分 我没有我的pg了,但我会去非常明确的URL的每一部分-
select regexp_replace('https://www.google.co.uk/cricket/hello', '.*\.[a-z]+\/', '')
测试:
'[^:]+:\/\/[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
以安德鲁·沃尔夫(Andrew Wolfe)对最奇怪的URL的查询为例
.*?\.[a-z]+\/
@Ashish-佩德罗的答案很适合你的例子-你只是想让这个例子奏效吗?否则,我建议您仔细考虑需求并更新您的问题(或者接受此答案并发布新问题)。顺便说一句,你的代码的问题似乎是
regexp\u replace
与贪婪匹配(最新的/
)。我还有其他URL,比如www.google.co.uk/cricket/hello,这个例子的答案不适用@steveklein关于贪婪的问题你是对的。虽然我使用了非贪婪版本,但是regexp\u replace
仍然是贪婪的。我如何解决这个问题。我已经更新了我的答案,试一试。它适用于任何gtld或cctld。@PedroLobito不起作用。结果还是打了个招呼。我相信GreenPlum有问题。你想做什么而不是打板球/你好?这是学校还是培训任务?看起来不错,但在GreenPlum上仍然不起作用。前四个给出hello作为结果,最后一个给出更长的?和&it=has&args
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