pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Leif B. Kristensen <leif@solumslekt.org>
To: pgsql-sql@postgresql.org
Subject: Plpgsql: Assign regular expression match to variable
Date: Tue, 1 Sep 2009 14:01:00 +0200
Message-ID: <200909011401.00231.leif@solumslekt.org> (raw)

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.
-- 
Leif Biberg Kristensen | Registered Linux User #338009
Me And My Database: http://solumslekt.org/blog/



view thread (3+ messages)  latest in thread

Message-ID: <200909011401.00231.leif@solumslekt.org>
Permalink:  ../200909011401.00231.leif@solumslekt.org/
Also on:    postgresql.org/message-id/200909011401.00231.leif@solumslekt.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: leif@solumslekt.org
  Subject: Re: Plpgsql: Assign regular expression match to variable
  In-Reply-To: <200909011401.00231.leif@solumslekt.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox