Re: RES: Using regexp_replace to remove small words

From: Robert Gravsjö <robert(at)blogg(dot)se>
To: Henrique de Lima Trindade <henrique(at)vivver(dot)com(dot)br>
Cc: 'Peter Eisentraut' <peter_e(at)gmx(dot)net>, pgsql-general(at)postgresql(dot)org, 'rodrigo mendonca' <rodrigo(at)vivver(dot)com(dot)br>
Subject: Re: RES: Using regexp_replace to remove small words
Date: 2010-12-13 15:04:26
Message-ID: 4D0635FA.10601@blogg.se
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 2010-12-13 13.24, Henrique de Lima Trindade wrote:
> Hi Peter,
>
> Your example works perfectly. But, I need Your help with on another situation.
> We're trying to create a plpgsql function with the expression. But, I'm getting a syntax error:
>
> -----------------
> create or replace function sp_remove_small_words( ptext text ) returns text immutable as
> $$
> begin
>
> return regexp_replace( ptext, $$\y\w{1,3}\y$$, '', 'g' );
>
> end;
> $$ language plpgsql
> ;
> -----------------
> ERRO: erro de sintaxe em ou próximo a "\"
> LINE 6: return regexp_replace( ptext, $$\y\w{1,3}\y$$, '', 'g' );

You're ending the function declaration with the $$. Using $fun$ as For
example, the following works:

create or replace function sp_remove_small_words( ptext text ) returns
text immutable as
$fun$
begin
return regexp_replace( ptext,
$$\y\w{1,3}\y$$, '', 'g' ); end;
$fun$
language plpgsql;

For details on dollar quoting see:
http://www.postgresql.org/docs/current/interactive/sql-syntax-lexical.html#SQL-SYNTAX-DOLLAR-QUOTING

Regards,
roppert
> ^
>
> ********** Error **********
>
> ERRO: erro de sintaxe em ou próximo a "\"
> SQL state: 42601
> Character: 138
>
> Thanks again!
>
>
> -----Mensagem original-----
> De: Peter Eisentraut [mailto:peter_e(at)gmx(dot)net]
> Enviada em: sexta-feira, 10 de dezembro de 2010 20:59
> Para: Henrique de Lima Trindade
> Cc: pgsql-general(at)postgresql(dot)org
> Assunto: Re: [GENERAL] Using regexp_replace to remove small words
>
> On fre, 2010-12-10 at 10:47 -0200, Henrique de Lima Trindade wrote:
>> I'm trying to find a regular expression that removes all small (length< N)
>> words from a string. But, until now I've not been successful.
>
> Here is a start:
>
> select regexp_replace('Tommy Lee Jones', $$\y\w{2,3}\y$$, ' ', 'g' );
>
> If you want to normalize the spaces after the removal and handle
> beginning and end of the word, you will need to expand this to cover
> those cases, but the example should contain the key ingredients.
>
>
>

--
Regards,
Robert "roppert" Gravsjö

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Vick Khera 2010-12-13 15:11:05 Re: Using regexp_replace to remove small words
Previous Message Tom Lane 2010-12-13 15:00:34 Re: Using regexp_replace to remove small words