Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gJj2A-0002RW-UI for pgsql-sql@arkaria.postgresql.org; Mon, 05 Nov 2018 17:51:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gJj28-0001CL-B0 for pgsql-sql@arkaria.postgresql.org; Mon, 05 Nov 2018 17:51:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gJj28-0001CE-4P for pgsql-sql@lists.postgresql.org; Mon, 05 Nov 2018 17:51:28 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gJj24-0005S6-TC for pgsql-sql@lists.postgresql.org; Mon, 05 Nov 2018 17:51:27 +0000 Received: from [192.168.127.1] (port=32497 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gJj21-000Pdy-OL; Mon, 05 Nov 2018 17:51:21 +0000 Received: from [127.0.0.1] (port=63358 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gJj20-000BWR-Uu; Mon, 05 Nov 2018 17:51:21 +0000 From: Andrew Gierth To: "Mark Williams" Cc: Subject: Re: Regular Expressions In-Reply-To: <012401d47472$0ebc9240$2c35b6c0$@gmail.com> (Mark Williams's message of "Sun, 4 Nov 2018 19:10:31 -0000") Message-ID: <878t272zlw.fsf@news-spur.riddles.org.uk> References: <012401d47472$0ebc9240$2c35b6c0$@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Mon, 05 Nov 2018 17:51:19 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Mark" == Mark Williams 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)