Re: Regexp_replace question / help needed

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Christopher Molnar <cmolnar(at)ourworldservices(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Regexp_replace question / help needed
Date: 2015-12-09 22:24:00
Message-ID: 12631.1449699840@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Christopher Molnar <cmolnar(at)ourworldservices(dot)com> writes:
> 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=')

There's more than one way to do it. You could use capturing parens:

regexp_replace('http://test.com/test/testfile.php','/([^/]*)$','&file=\1')

or you could use a lookahead constraint:

regexp_replace('http://test.com/test/testfile.php','/(?=[^/]*$)','&file=')

regards, tom lane

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Nicolas Paris 2015-12-09 22:25:24 Re: Regexp_replace question / help needed
Previous Message Christopher Molnar 2015-12-09 21:58:45 Regexp_replace question / help needed