Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:
    links.extend(links_in_message)

And for example this string:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

string = 'Hello www.fb.com/home how are you https://twitter.it/home ?'

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

//Result = 'Hello www.rere.me/home how are you https://rere.me/home ?'

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading