Re: Need a help in regexp

From: Leif Biberg Kristensen <leif(at)solumslekt(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: Need a help in regexp
Date: 2010-05-06 15:37:52
Message-ID: 201005061737.52660.leif@solumslekt.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general pgsql-sql

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/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Alvaro Herrera 2010-05-06 15:42:36 Re: pre-existing shared memory block is still in use after crashes
Previous Message Thom Brown 2010-05-06 15:30:28 Re: I need to take metadata from a shell script.

Browse pgsql-sql by date

  From Date Subject
Next Message Plugge, Joe R. 2010-05-06 20:12:32 Column Specific Update Trigger Routine
Previous Message A. Kretschmer 2010-05-06 15:04:47 Re: Need a help in regexp