Received: from maia.hub.org (maia-1.hub.org [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id 889196337BC for ; Thu, 6 May 2010 12:38:04 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 34303-03 for ; Thu, 6 May 2010 15:37:47 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from mail43.e.nsc.no (mail43.e.nsc.no [193.213.115.43]) by mail.postgresql.org (Postfix) with ESMTP id 94B5963340A for ; Thu, 6 May 2010 12:37:55 -0300 (ADT) Received: from balapapa.localnet (42.185.16.62.customer.cdi.no [62.16.185.42]) (authenticated bits=0) by mail43.nsc.no (8.14.3/8.14.3) with ESMTP id o46Fbril017066 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT) for ; Thu, 6 May 2010 17:37:54 +0200 (MEST) From: Leif Biberg Kristensen To: pgsql-sql@postgresql.org Subject: Re: Need a help in regexp Date: Thu, 6 May 2010 17:37:52 +0200 User-Agent: KMail/1.12.4 (Linux/2.6.32-gentoo-r7; KDE/4.3.5; i686; ; ) References: In-Reply-To: MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201005061737.52660.leif@solumslekt.org> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201005/23 X-Sequence-Number: 34071 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/