Re: regexp_replace question/suggestions needed

From: Corey Huinker <corey(dot)huinker(at)gmail(dot)com>
To: Christopher Molnar <cmolnar65(at)gmail(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: regexp_replace question/suggestions needed
Date: 2015-12-14 20:40:41
Message-ID: CADkLM=cs=pasjJFT3iX_KrK4vh2TRo+W5rdSnxyCHR2vgnXU7w@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Wed, Dec 9, 2015 at 4:57 PM, Christopher Molnar <cmolnar65(at)gmail(dot)com>
wrote:

> Hello,
>
> I am running into a problem and need some pointers on regexp_replace - I
> can't seem to find an answer in any of the online resources.
>
> I have a string (like 40,000 with different length and number of
> components) of them in a field named "externalurl". I need to replace the
> final "/" of the string with "&file=" while preserving the filename and
> extension following the "/".
>
> The closest I can get is:
>
> regexp_replace('http://test.com/test/testfile.php','/[^/]*$','&file=')
>
> however this looses the file name and returns:
>
> http://test.com/test&file=
>
> What I am looking for is:
>
> http://test.com/test&file=testfile.php
>
> as a result.
>
> Would anyone here point me in the right direction?
>
> Thanks!
> -Chris
>

Not the most elegant, but this seems to do the trick

# select regexp_replace('http://test.com/test/testfile.php','/[^/]*$','')
|| '&file=' || regexp_replace('http://test.com/test/testfile.php','.*/','')
;
?column?
----------------------------------------
http://test.com/test&file=testfile.php
(1 row)

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Hector Vass 2015-12-14 21:47:33 Re: regexp_replace question/suggestions needed
Previous Message Michael Moore 2015-12-10 19:51:58 Re: CVS to In_list without dynamic SQL, how?