user6089877
user6089877

Reputation:

Replace occurrence in Python

I have a string and this string could contain some occurrences like:

http://site/image.jpg

What is the right way to replace, when it's present, this kind of occurrence by

<img src="http://site/image.jpg">

What is really important, is to replace only occurrences beginning by http and which ending by .jpg, .png and gif by the <img> HTML tag.

So if among all the text there is a URL link of any images, it's formatted by a HTML tag to display the image.

Upvotes: 1

Views: 54

Answers (2)

Luis Filipe
Luis Filipe

Reputation: 11

This is a simple answer to your question:

def check_if_image(url, image_extensions):
   if url.startswith("https://") or url.startswith("http://"):
       for extension in image_extensions:
           if(extension in url[-4:]):
               return True
   return False

def main():
   url_seed = ["http://somesite.com/img1.jpg", "https://somesite2.com/img2.gif", 
            "http://somesite3.net/img3.png", "http://noimagesite.com/noimage"]
   image_extensions = [".jpg", ".png", ".gif"]

   final_result=[]
   for site in url_seed:
       if check_if_image(site, image_extensions):
           final_result.append('<img src="%s">' %site)
   print(final_result)

This includes "http" and "https" site verification, as well as code working for 3 character image extensions, such as you asked: jpg, gif and png.

Hope it helped. Feel free to ask if you have any question.

Edit: Didn't notice you had not the urls already in a data structure, so this is invalid to your situation

Upvotes: 1

DeepSpace
DeepSpace

Reputation: 81604

Pretty straightforward with regex:

import re

string = 'some other text, a URL http://site/image.jpg and other text'

print(re.sub(r'(https?.+?(?:jpg|png|gif))', r'<img src="\1">', string))

# some other text, a URL <img src="http://site/image.jpg"> and other text

(https?.+(?:jpg|png|gif)) matches everything that starts with http or https and ending with jpg, png or gif.

'<img src="\1">' here the \1 refers to the first (and only) capture group in the previous regex (which contains the image url).

Upvotes: 1

Related Questions