pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Leif Biberg Kristensen <leif@solumslekt.org>
To: pgsql-sql@postgresql.org
Subject: Re: Need a help in regexp
Date: Thu, 6 May 2010 17:37:52 +0200
Message-ID: <201005061737.52660.leif@solumslekt.org> (raw)
In-Reply-To: <k2oe13d0d11005060748l4eaf0ac2g9b1db464c524b8e4@mail.gmail.com>
References: <k2oe13d0d11005060748l4eaf0ac2g9b1db464c524b8e4@mail.gmail.com>

On Thursday 6. May 2010 16.48.26 Nicholas I wrote:
> Hi,
> 
> Need a help in regexp!
> 
> I have a table in which the data's are entered like,
> 
> Example:
> 
> One (1)
> Two (2)
> Three (3)
> 
> I want to extract the data which is only within the parentheses.
> 
> that is
> 1
> 2
> 3
> 
> i have written a query,
> *select regexp_matches(name,'([^(]+)([)]+)','g') from table;*
> which outputs the data as,
> {"test"}
> {"test2"}

If what you've got inside the parentheses always is an integer, and it's 
always the only or first integer in the string, you can use:

SELECT (REGEXP_MATCHES(bar, E'(\\d+)'))[1] FROM foo;

You can even cast it to an integer on the fly:

SELECT (REGEXP_MATCHES(bar, E'(\\d+)'))[1]::INTEGER FROM foo;

Or as a more general case, whatever's inside (the first) set of parentheses:

SELECT (REGEXP_MATCHES(bar, E'\\((.+?)\\)'))[1] FROM foo;

regards,
-- 
Leif Biberg Kristensen
http://solumslekt.org/blog/



view thread (4+ messages)

Message-ID: <201005061737.52660.leif@solumslekt.org>
Permalink:  ../201005061737.52660.leif@solumslekt.org/
Also on:    postgresql.org/message-id/201005061737.52660.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: Need a help in regexp
  In-Reply-To: <201005061737.52660.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