Re: Help with a not match - Mailing list pgsql-sql

From Andrew Gierth
Subject Re: Help with a not match
Date
Msg-id 87a7mix73u.fsf@news-spur.riddles.org.uk
Whole thread Raw
In response to Re: Help with a not match  ("Campbell, Lance" <lance@illinois.edu>)
List pgsql-sql
>>>>> "Campbell" == Campbell, Lance <lance@illinois.edu> writes:

 Campbell> Very helpful.  I am almost there.  
 Campbell> I created this SQL:

 Campbell> SELECT regexp_matches(content, '/(?!files/'||id||'/)(files/\d+/)/', 'g') FROM tablea

 Campbell> I get no matches. My guess is I am close but slightly off on
 Campbell> the syntax.

Simplest regexp solution is to do this:

SELECT ... WHERE content ~ ('files/(?!' ||id|| '/)\d+/')

i.e. we're generating a regexp like 'files/(?!123/)\d+/' for each row.
No need for regexp_matches in this case because all we're looking for is
whether a match exists.

Another, possibly faster because it doesn't need a regexp compile for
each row, but possibly slower due to subplan overhead, would be:

SELECT ...
 WHERE id::text <> ANY (SELECT (regexp_matches(content, 'files/(\d+)/', 'g'))[1])

The idea of the second method is to extract all the "NNN" values from
files/NNN/ substrings, and then test whether any NNN value is different
from the expected one. (This is a VERY RARE use of "<> ANY"; normally
one uses "<> ALL" as the negation of "= ANY", but the logic here
requires the negation of "= ALL" instead.)

-- 
Andrew (irc:RhodiumToad)


pgsql-sql by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: Help with a not match
Next
From: Andreas Joseph Krogh
Date:
Subject: Difficulties with LAG-function when calculating overtime