Re: Regular Expressions - Mailing list pgsql-sql

From Andrew Gierth
Subject Re: Regular Expressions
Date
Msg-id 878t272zlw.fsf@news-spur.riddles.org.uk
Whole thread Raw
In response to Regular Expressions  ("Mark Williams" <markwillimas@gmail.com>)
List pgsql-sql
>>>>> "Mark" == Mark Williams <markwillimas@gmail.com> writes:

 Mark> I can't figure out how to search myfield for all instances which
 Mark> contain "text1" AND "text2".

I should start by pointing out that (as mentioned by other people) using
regexps is not necessarily the best way to do this, especially not when
dealing with actual words which is what FTS exists for.

But a solution does exist (at least in pg and other regexp engines that
support lookahead assertions):

myfield ~* '^(?=.*\mtext1\M)(?=.*\mtext2\M)'

What this says is: match at the start of the string if (and only if)
both the lookahead assertions succeed; since neither assertion advances
the match, they will find the two specified words regardless of the
order in which they appear. (The trick of using | to search for both
possible orders works for 2 words, but gets unwieldy very quickly with
more; with the assertion method you can handle any number of words.)

-- 
Andrew (irc:RhodiumToad)


pgsql-sql by date:

Previous
From: "David G. Johnston"
Date:
Subject: Re: multiple roles for a user ?
Next
From: "Campbell, Lance"
Date:
Subject: Help with a not match