Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does postgresql support lookbehind regexp?

If yes, pls provide an example for lookbehind or an alternative.

I'm trying to extract the sequence name without '

select table_name,
       column_name,
       regexp_replace(substring(column_default from '''.*(?='')'),'''','','g') as sequence
FROM information_schema.columns 
like image 864
johnlemon Avatar asked Aug 03 '11 07:08

johnlemon


People also ask

Does Postgres support regex?

PostgreSQL employs Regular Expressions to get around pattern matching. In this article, we will learn about PostgreSQL Regex. PostgreSQL uses POSIX or “Portable Operating System Interface for Unix” regular expressions, which are better than LIKE and SIMILAR TO operators used for pattern matching.

Can I use regex Lookbehind?

The good news is that you can use lookbehind anywhere in the regex, not only at the start.

What is lookahead and Lookbehind?

The lookbehind asserts that what immediately precedes the current position is a lowercase letter. And the lookahead asserts that what immediately follows the current position is an uppercase letter.

How can you compare a part of name rather than entire name in PostgreSQL?

The % operator lets you compare against elements of an array, so you can match against any part of the name.


2 Answers

As of 9.6, it does accept positive lookbehinds using (?<=re), documentation here.

like image 173
João Ciocca Avatar answered Oct 02 '22 02:10

João Ciocca


It didn't support it at the sql level last I checked, but you can use plperl to work around the limitation if absolutely necessary. (Heavy regexing generally doesn't belong at the DB level, though...)

In your particular example, consider using a negative class instead: [^'] (escape it as needed), or a non-greedy wildcard: .*?.

Adding to this re your specific question, unless you actually create your sequence manually its name will always be:

tablename_colname_seq

Also FWIW, the following two defaults have a different behavior if you use multiple schemas and search paths in your app:

nextval('foo'::regclass)  -- find foo once
nextval('foo'::text)      -- find foo each time
like image 37
Denis de Bernardy Avatar answered Oct 02 '22 03:10

Denis de Bernardy