moonshot
moonshot

Reputation: 699

Simple Negative Lookahead in Postgres 9.5 Not Working

I'm tryin to match any string containing STORE when it's NOT followed by HOUSE. I looked at this answer Postgres Regex Negative Lookahead but cannot get it to work with my example.

Here is the sql demo and below is my code:

CREATE TABLE table1
    (s character varying)
;

INSERT INTO table1
    (s)
VALUES
    ('FROM THE STORE TO THE HOUSE AND'),
    ('FROM THE HOUSE TO THE STORE AND')
;
select * from table1 where s ~ '(STORE)(?!HOUSE)';

UPDATE Had wrong link for SQL demo - it is fixed now

Upvotes: 2

Views: 379

Answers (1)

Toto
Toto

Reputation: 91508

Your regex matches STORE only if it is not immediatly followed by HOUSE.

I guess you want:

(STORE)((?!HOUSE).)*$

This will match STORE in the second row of your example.

Upvotes: 3

Related Questions