Could you not use a simple capture and a greedy * ...
Should deal with your example
select regexp_replace('http://test.com/test/testfile.php','(.*)/(.*)','\1&file=\2');
and as requested <with different length and number of components>
select regexp_replace('http://test.com/otherstuff/in/path/test/testfile.php','(.*)/(.*)','\1&file=\2');
Hector Vass
07773 352 559
01666 820 008
MetaMetrics, International House, 107 Gloucester Road, Malmesbury, Wiltshire, SN16 0AJ
This e-mail and any attachments are confidential and for the attention of the addressee only. If you are not the intended recipient, any use, disclosure or copying of this document is unauthorised. If you have received this document in error please immediately notify the sender and delete this e-mail from your system. Whilst all emails sent by MetaMetrics are scanned using up-to-date virus scanning software, MetaMetrics Ltd. accepts no liability for any loss or damage which may be caused by software viruses and recommend that you conduct your own virus checks on all attached materials. Please note that any attached materials remain the exclusive property of MetaMetrics Ltd. unless expressly stated otherwise. Metametrics Limited is a limited company registered in England & Wales. Registered number 05453613. Registered offices at 86 Shirehampton Road, Stoke Bishop, Bristol, BS9 2DR
From: pgsql-sql-owner@postgresql.org <pgsql-sql-owner@postgresql.org> on behalf of Corey Huinker <corey.huinker@gmail.com>
Sent: 14 December 2015 20:40
To: Christopher Molnar
Cc: pgsql-sql@postgresql.org
Subject: Re: [SQL] regexp_replace question/suggestions needed