Обсуждение: Need a help in regexp

Поиск
Список
Период
Сортировка

Need a help in regexp

От
Nicholas I
Дата:
<span style="display: block; padding-left: 6em;"><span></span></span>Hi,<br /><br />Need a help in regexp!<br /><br />I
havea table in which the data's are entered like,<br /><br />Example:<br /><br />One (1)<br />Two (2)<br />Three (3)<br
/><br/>I want to extract the data which is only within the parentheses.<br /><br />that is <br />1<br />2<br /> 3<br
/><br/>i have written a query, <br /><b>select regexp_matches(name,'([^(]+)([)]+)','g') from table;</b><br />which
outputsthe data as, <br />{"test"}<br />{"test2"}<br /><span style="display: block; padding-left:
6em;"><span></span></span><br/><br />Thank You<br /><font color="#888888">Nicholas I</font><span style="display: block;
padding-left:6em;"><span></span></span><span style="display: block; padding-left: 6em;"><span></span></span> 

Re: Need a help in regexp

От
"A. Kretschmer"
Дата:
In response to Nicholas I :
> 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"}
> 
> 
> Thank You
> Nicholas I


test=*# select * from regex ;    t
-----------one (1)two (2)three (3)
(3 Zeilen)

Zeit: 0,262 ms

test=*# select regexp_replace(t, '[^0-9]','','g') from regex;regexp_replace
----------------123
(3 Zeilen)


Regards, Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG: 0x31720C99, 1006 CCB4 A326 1D42 6431  2EB0 389D 1DC2 3172 0C99


Re: Need a help in regexp

От
Leif Biberg Kristensen
Дата:
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/