Sublime Forum

Regex Backreference Conditional Replace

#1

Hi,

I am trying to replace a url with some code, however sometimes the source url will contain a # and extra information and sometimes it won’t.

I only want to insert

First URL with hash: someurl.com/test/#/testwithhash

Should be after replacement:
%%=RedirectTo(CONCAT('http://’,@SUBDOMAIN,‘someurl.com/test/’, CHAR(35), ‘/testwithhas/’))=%%

Second URL without hash: someurl.com/testnohash/

Should be after replace:
%%=RedirectTo(CONCAT('http://’,@SUBDOMAIN,‘someurl.com/testnohash/’))=%%

So far I have this:
Find: (https?://)(\w./?=]+)(#?)(\w./?=]+)
Replace: %%=RedirectTo(CONCAT(’$1’,@SUBDOMAIN,’$2’, CHAR(35), ‘$4’))=%%

This works for the first URL but adds an unwanted CHAR(35) for the second url.

But need to add some conditional formatting as even on the second URL I will insert a # CHAR(35)

I have seen a few mentions that suggest the replace supports backreferences but they don’t see to work for me, am I missing something?

Thanks
A

0 Likes