Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 6B867631E2D for ; Tue, 1 Sep 2009 09:36:31 -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 91892-07 for ; Tue, 1 Sep 2009 12:36:13 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from qw-out-2122.google.com (qw-out-2122.google.com [74.125.92.25]) by mail.postgresql.org (Postfix) with ESMTP id 9744A6338B6 for ; Tue, 1 Sep 2009 09:36:20 -0300 (ADT) Received: by qw-out-2122.google.com with SMTP id 3so1107694qwe.61 for ; Tue, 01 Sep 2009 05:36:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type; bh=OR//4cheSfp0a9ZE1qoHisnNV1Npbs1LNZIQEPDS9wo=; b=gkHJ3iGuE/z5joAbsoTwM2bU0aYYbjO4OBR0SF0BbR4EKKa4F4Vg7MAZhqpYUr6Png iQYB0dwnYCidFdAT7pi6BGgO1xQ0Nw78TCL8uzCc4asOGxdd8ROsbLCgfSB7+UCYnbrl qSY8IBDTCNeK6CQX9BqTVIhtMK/hW2m29Nk1I= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=kJ4w1w8cKromtO/95hAtuNcKDkoVHLN9RtSD7pYyTgkD2kmqBQXD1SZy15yiu42S2h VqHnClFOjqQ/hoXpnuezSyMzXBXUEg++3hkSyU6rm3tHAKMB26DqTi2+NvYnDP7HQKp6 2aHdkJbaQoWQfdiH9GW+FgfZIcwrvb2B2A69A= MIME-Version: 1.0 Received: by 10.229.27.195 with SMTP id j3mr1976561qcc.57.1251808578862; Tue, 01 Sep 2009 05:36:18 -0700 (PDT) In-Reply-To: <200909011401.00231.leif@solumslekt.org> References: <200909011401.00231.leif@solumslekt.org> Date: Tue, 1 Sep 2009 21:36:18 +0900 Message-ID: <1d581afe0909010536g6fdb57a0u87c8d748fb9067b6@mail.gmail.com> Subject: Re: Plpgsql: Assign regular expression match to variable From: Ian Barwick To: "Leif B. Kristensen" Cc: pgsql-sql@postgresql.org Content-Type: text/plain; charset=UTF-8 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.11 tagged_above=-10 required=5 tests=BAYES_05=-1.11 X-Spam-Level: X-Archive-Number: 200909/3 X-Sequence-Number: 33068 2009/9/1, Leif B. Kristensen : > In Plpgsql, I've got this problem of how to assign an integer extracted > from a regex to a variable. My approach so far feels kludgy: > > -- extract ^#(\d+) from txt > IF txt SIMILAR TO E'#\\d+%' THEN > my_int := SUBSTR(SUBSTRING(txt, E'#\\d+'), 2, > LENGTH(SUBSTRING(txt, E'#\\d+')) -1)::INTEGER; > -- strip ^#\d+ from text > my_txt := REGEXP_REPLACE(txt, E'^#\\d+ ', ''); > END IF; > > What I'd like to do is something like this: > > my_int := MATCH(txt, '^#(\d+)')::INTEGER; > > which would assign the integer atom (\d+) to my_int. This seems to do what you want: my_int := (REGEXP_MATCHES(txt, E'^#(\\d+)'))[1]; Ian Barwick