Waiting for PostgreSQL 15 – Add assorted new regexp_xxx SQL functions.

On 3rd of August 2021, Tom Lane committed patch:

Add assorted new regexp_xxx SQL functions.
 
This patch adds new functions regexp_count(), regexp_instr(),
regexp_like(), and regexp_substr(), and extends regexp_replace()
with some new optional arguments.  All these functions follow
the definitions used in Oracle, although there are small differences
in the regexp language due to using our own regexp engine -- most
notably, that the default newline-matching behavior is different.
Similar functions appear in DB2 and elsewhere, too.  Aside from
easing portability, these functions are easier to use for certain
tasks than our existing regexp_match[es] functions.
 
Gilles Darold, heavily revised by me
 
Discussion: https://postgr.es/m/fc160ee0-c843-b024-29bb-97b5da61971f@darold.net

I missed it. Sorry. But found it after almost 4 months, and decided to write about.

I'm huge fan of regexps. Interestingly – I'm not fan of regexps in Pg, as the regexp engine in PostgreSQL has weirdish limits that don't sit well with me, especially since there are so many other options.

Anyway, up to Pg14, we had these regexp functions:

  • regexp_match
  • regexp_matches
  • regexp_replace
  • regexp_split_to_array
  • regexp_split_to_table

Now, we got 4 more:

regexp_count( string, pattern [, start [, flags]]

Returns how many times given pattern matches in source string, optionally with index that means that it should start searching from n-th characters in a string, and with some optional flags.

For example:

=$ select regexp_count('depesz DEPESZ depesz DepEsz', 'de.e');
 regexp_count 
--------------
            2
(1 row)
 
=$ select regexp_count('depesz DEPESZ depesz DepEsz', 'de.e', 2);
 regexp_count 
--------------
            1
(1 row)
 
=$ select regexp_count('depesz DEPESZ depesz DepEsz', 'de.e', 1, 'i');
 regexp_count 
--------------
            4
(1 row)

regexp_instr( string, pattern [, start [, N [, endoption [, flags [, subexpr ]]]]])

This one is even more complex, with more optional parameters.

In it's simplest form it returns position at which pattern starts in string:

=$ with input as (select 'abcdefghijklmnopqrstuvwxyz'::text as i),
ri as (select regexp_instr( i, '..[aeiou]' ) as p from input)
select ri.p, substr( input.i, ri.p) from input, ri;
 p |          substr          
---+--------------------------
 3 | cdefghijklmnopqrstuvwxyz
(1 row)

Second argument is simply character you want to start working from. So if I'd say I want to start from 4th:

=$ with input as (select 'abcdefghijklmnopqrstuvwxyz'::text as i),
ri as (select regexp_instr( i, '..[aeiou]', 4 ) as p from input)
select ri.p, substr( input.i, ri.p ) from input, ri;
 p |        substr        
---+----------------------
 7 | ghijklmnopqrstuvwxyz
(1 row)

Third is which match should regexp_instr consider for position calculation. By default it starts from first match, but we can easily make it look for third:

=$ with input as (select 'abcdefghijklmnopqrstuvwxyz'::text as i),
ri as (select regexp_instr( i, '..[aeiou]', 1, 3 ) as p from input)
select ri.p, substr( input.i, ri.p) from input, ri;
 p  |     substr     
----+----------------
 13 | mnopqrstuvwxyz
(1 row)

Fourth option is endoption. It can be only 0/null/1, and if it's set to 1, regexp_instr will return position of first character after given match:

=$ with input as (select 'abcdefghijklmnopqrstuvwxyz'::text as i),
ri as (select regexp_instr( i, '..[aeiou]', 1, 3, 1 ) as p from input)
select ri.p, substr( input.i, ri.p) from input, ri;
 p  |   substr    
----+-------------
 16 | pqrstuvwxyz
(1 row)

This leads to interesting question -what will it show for regexp that doesn't match?

=$ select regexp_instr('aaa', 'b', 1, 1, 1);
 regexp_instr 
--------------
            0
(1 row)

and, obviously, what will happen if it matches at the end?

=$ select regexp_instr('abc', 'c', 1, 1, 1);
 regexp_instr 
--------------
            4
(1 row)

Fifth argument is flags, which I mentioned earlier with regexp_count, but sixth is a new thing: subexpr. What does that mean?

It means that if my pattern contains elements in parens I can ask for specific set of parens:

=$ with input as (select 'abcdefghijklmnopqrstuvwxyz'::text as i),
ri as (select regexp_instr( i, '.(.[aeiou])(..)', 1, 1, 0, '') as p from input)
select ri.p, substr( input.i, ri.p) from input, ri;
 p |          substr          
---+--------------------------
 3 | cdefghijklmnopqrstuvwxyz
(1 row)
 
=$ with input as (select 'abcdefghijklmnopqrstuvwxyz'::text as i),
ri as (select regexp_instr( i, '.(.[aeiou])(..)', 1, 1, 0, '', 1) as p from input)
select ri.p, substr( input.i, ri.p) from input, ri;
 p |         substr          
---+-------------------------
 4 | defghijklmnopqrstuvwxyz
(1 row)
 
=$ with input as (select 'abcdefghijklmnopqrstuvwxyz'::text as i),
ri as (select regexp_instr( i, '.(.[aeiou])(..)', 1, 1, 0, '', 2) as p from input)
select ri.p, substr( input.i, ri.p) from input, ri;
 p |        substr         
---+-----------------------
 6 | fghijklmnopqrstuvwxyz
(1 row)

Complex. And shows that we really could use named arguments for built-in functions. I'd much rather see:

select regexp_instr( 'abcdefghijklmnopqrstuvwxyz', '.(.[aeiou])(..)', subexpr => 2 )

than

select regexp_instr( 'abcdefghijklmnopqrstuvwxyz', '.(.[aeiou])(..)', 1, 1, 0, '', 2 )

regexp_like( string, pattern [, flags])

This time simple true/false whether given pattern matches string. With optional flags:

=$ select regexp_like( 'AAA', 'a+' ), regexp_like( 'AAA', 'a+', 'i');
 regexp_like | regexp_like 
-------------+-------------
 f           | t
(1 row)

regexp_substr( string, pattern [, start [, N [, flags [, subexpr ]]]])

This is somewhat similar to regexp_instr, at least when it comes to options that it gets. Aside from endoption, all the other are the same, and work the same way.

The difference is that instead of returning position, it will return what was matched. Including all selectors like start position, n-th match, flags, and subexpression number:

=$ select n as nth, s as subexpression, regexp_substr( 'abcdefghijklmnopqrstuvwxyz', '.(.[aeiou])(..)', 1, n, '', s )
from generate_series(1,3) n, generate_series(0, 2) s;
 nth | subexpression | regexp_substr 
-----+---------------+---------------
   1 |             0 | cdefg
   1 |             1 | de
   1 |             2 | fg
   2 |             0 | mnopq
   2 |             1 | no
   2 |             2 | pq
   3 |             0 | stuvw
   3 |             1 | tu
   3 |             2 | vw
(9 rows)

Additionally, to the four new functions we also got extension to:

regexp_replace(source, pattern, replacement [, start [, N ]] [, flags ])

Two new options were added – start and N, which, just like with other functions mean that matching should considering string from character number START, and work on Nth match, so I can:

=$ select regexp_replace('hubert xxx lubaczewski', '(\S+)', 'depesz', 1, 2);
      regexp_replace       
---------------------------
 hubert depesz lubaczewski
(1 row)

Pretty handy. Thanks a lot to all involved, and sorry for lag with description.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.