Python Regex replacement with sequential replacements from list of value

I have a big problem with this regex.

I have a string, which can contain multiple and differents links anywhere inside. I need to take those links and make a list of them, then i elaborate them with an url shortener. Then have to replace them sequentially in the string with the new link i have.
For the first part i’ve done this:

links = []
links_in_message = re.findall(r'(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})', message.text)
if links_in_message:

And for example this string:

string = 'Hello how are you ?'

should become (where the link are not a substitution of the domain with, but every link is taken sequentially from my links list):

//Result = 'Hello how are you ?'

I’m thinking about deleting the links from the string and help me saving link index in string to compose a new string but i was wondering if there was another way. Thank you.

>Solution :

You could use string.replace since you have the exact text (links) that you want to replace.

For example:

for link in links:
   string = string.replace(link, shorten_link(link), 1)

Leave a Reply