Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 25090633C4E for ; Fri, 8 May 2009 02:26:27 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 78597-02 for ; Fri, 8 May 2009 02:26:23 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [202.61.230.242]) by mail.postgresql.org (Postfix) with ESMTP id 7DEFB632C17 for ; Fri, 8 May 2009 02:26:25 -0300 (ADT) Received: from mail.postnewspapers.com.au (202-89-185-120.static.dsl.amnet.net.au [202.89.185.120]) by www.postnewspapers.com.au (Postfix) with ESMTP id A4EE65C0AA for ; Fri, 8 May 2009 13:26:21 +0800 (WST) Received: from localhost (access [127.0.0.1]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 833B61E0437 for ; Fri, 8 May 2009 13:26:21 +0800 (WST) X-Virus-Scanned: Debian amavisd-new at postnewspapers.com.au Received: from mail.postnewspapers.com.au ([127.0.0.1]) by localhost (access.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id LlUEYdlQkpAd for ; Fri, 8 May 2009 13:26:19 +0800 (WST) Received: from [119.11.22.165] (unknown [119.11.22.165]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "Craig Ringer", Issuer "POST Certificate Authority" (verified OK)) by mail.postnewspapers.com.au (Postfix) with ESMTP id B21F71E0436 for ; Fri, 8 May 2009 13:26:18 +0800 (WST) Message-ID: <4A03C276.9050007@postnewspapers.com.au> Date: Fri, 08 May 2009 13:26:14 +0800 From: Craig Ringer User-Agent: Thunderbird 2.0.0.21 (X11/20090409) MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: select regexp_matches('a a a', '([a-z]) a','g'); X-Enigmail-Version: 0.95.7 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=1.117 tagged_above=0 required=5 tests=RCVD_IN_SORBS_WEB=1.117 X-Spam-Level: * X-Archive-Number: 200905/18 X-Sequence-Number: 32510 First: Please don't reply to an existing message to create a new thread. Your mail client copies the replied-to message ID into the References: header, and well-implemented mail clients will thread your message under a now-unrelated thread. Compose a new message instead. Marc Mamin wrote: > I have a string that contains a serie of chars, separated by single > spaces. > > e.g 'a b x n r a b c b' > > Having such a string, I d'like to get a list of all predecessors of a > given character. > In the example, the predecessors of b are a,a,c. OK, so wherever `b' occurs, you want the character at index `b -2'. > select regexp_matches('a a a', '([a-z]) a','g'); > => {"a "} only The issue is that regular expressions don't like to overlap matches. The first match consumes _two_ leading `a' characters. What you need is a zero-width lookahead assertion, available in Perl-style extended regular expressions. Handily, recent PostgreSQL versions support these, so you can write: test=> select regexp_matches( 'a a a', '([a-z]) (?=a)', 'g'); regexp_matches ---------------- {a} {a} (2 rows) -- Craig Ringer