为什么此正则表达式在PostgreSQL中不起作用 [英] Why won't this regex work in postgreSQL

查看:109
本文介绍了为什么此正则表达式在PostgreSQL中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取正则表达式以从URL字符串捕获基本URL。这

I'm trying to get a regex to capture the base URL from a URL string. This

^(.+?[^\/:])(?=[?\/]|$)

有效。 REGEX101

但是当我尝试在PostgreSQL中使用

But when I try to use it within postgresql

regexp_replace(content_url,'^(.+?[^\\/:])(?=[?\\/]|$)', '\1') 

it不会

推荐答案

RegexBuddy 发出有关第一个'?'的警告

RegexBuddy gives this warning about the first '?'


PostgreSQL在处理$中的惰性量词的方式上不一致b $ b正则表达式具有交替性,因为它尝试匹配最长的
替代方案,而不是急于接受第一个匹配的
替代方案

PostgreSQL is inconsistent in the way it handles lazy quantifiers in regular expressions with alternation because it attempts to match the longest alternative, instead of being eager and accepting the first alternative that matches

,如果将其删除,它似乎可以工作,即 ^(。+ [^ \ /:])(?= [?\ /] | $)

and if you remove it, it seems to work, i.e ^(.+[^\/:])(?=[?\/]|$)

但是,如果您要解析正则表达式无法正常工作的基本URL。改用它:

however, if you're trying to parse the baseurl that regex won't work. Use this instead:

select regexp_replace('....', '^(.*:)//([a-z\-.]+)(:[0-9]+)?(.*)$', '\2')

这篇关于为什么此正则表达式在PostgreSQL中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆