Re: Regular Expressions

Поиск
Список
Период
Сортировка
От Andrew Gierth
Тема Re: Regular Expressions
Дата
Msg-id 878t272zlw.fsf@news-spur.riddles.org.uk
обсуждение исходный текст
Ответ на Regular Expressions  ("Mark Williams" <markwillimas@gmail.com>)
Список 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 по дате отправления:

Предыдущее
От: "David G. Johnston"
Дата:
Сообщение: Re: multiple roles for a user ?
Следующее
От: "Campbell, Lance"
Дата:
Сообщение: Help with a not match