Re: SIMILAR TO expressions translate wildcards where they shouldn't

Michael Paquier <michael@paquier.xyz>

From: Michael Paquier <michael@paquier.xyz>
To: Laurenz Albe <laurenz.albe@cybertec.at>
Cc: pgsql-bugs@lists.postgresql.org
Date: 2025-05-23T01:10:04Z
Lists: pgsql-bugs
On Thu, May 22, 2025 at 11:18:44PM +0200, Laurenz Albe wrote:
> The underscore before the [:alpha:] is left alone, but the one after
> it gets translated to a period.  Now the underscore is a wildcard
> that corresponds to the period in regular expressions, but characters
> in square brackets should lose their special meaning.  The code in
> utils/adt/regexp.c doesn't expect that square brackets can be nested.
> 
> The attached patch fixes the bug.

Oh, good catch.  [_[:alpha:]] and [[:alpha:]_] both that this should
match every string made of a-zA-Z and underscores, but this is failing
to do the job for the latter.

+           if (pchar != '^' && charclass_start)
+               charclass_start = false;

I'm a bit puzzled by this part about '^', though, resetting the fact
that we are in a squared bracket section with '^' treated as an
exception.  Perhaps this deserves a comment?
--
Michael

Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Adjust regex for test with opening parenthesis in character classes

  2. Fix conversion of SIMILAR TO regexes for character classes