且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么此正则表达式在PostgreSQL中不起作用

更新时间:2022-11-12 09:24:23

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')