I'm trying to affilliateize some legacy text in a Django webapp. It's a pretty simple scope. The text has some amazon URLs in and I want to munge my ?tag=xxx
identifier onto the end of them.
I've written a template filter that I can quickly pass my text through but I'm slightly stuck on writing the regex logic.
t = text_from_template_engine
return re.sub(r'(https?://(?:www\.)?amazon\.co\.uk[\S]+)', r'\\\1?tag=xxx', t)
This seems to work on a very basic level but if the URL already has a querystring (as lots of organic Amazon URLs do by default), I would need an ampersand instead of a question mark.
There might be a quick way to detect two question marks and replace the second. I'm open to that suggestion.
What I'm really looking for is a regex-replace where I can pass the found string off to another method (in which I can detect existing question marks) that is expected to return the replacement string. Something like PHP's preg_replace_callback
(et al). Does that exist?