python-3.ximage-text

how to get the starting text and ending text from image path in python using regex?


Here is the given two scenarios

Example 1

the Image Path is https://ictagrisindh.gov.pk/img/inauguration1.jpg the detail goes here
and the url was this and Click here to view the detail goes here

Example 2

https://ictagrisindh.gov.pk/img/inauguration1.jpg the detail goes here
Click here to view screenshot the detail goes here

My Code is Given Below

import re
 str_text = "the Image Path is https://ictagrisindh.gov.pk/img/inauguration1.jpg the detail goes here and the url was this and Click here to view the detail goes here"
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', str_text)
print("Urls: ",":".join(urls))

Result

https://ictagrisindh.gov.pk/img/inauguration1.jpg

i want to extract the text from starting point to end point between & also extract text from everywhere in image path

Any Help would be appreciated & thanks in Advance


Solution

  • import re
    
    e1 = 'the Image Path is https://ictagrisindh.gov.pk/img/inauguration1.jpg the detail goes here' + \
    'and the url was this and Click here to view the detail goes here'
    
    e2 = 'https://ictagrisindh.gov.pk/img/inauguration1.jpg the detail goes here' + \
    'Click here to view screenshot the detail goes here'
    
    start_pattern = '(^.+)(?=http.+.jpg)'
    image_url_pattern = '(http.+.jpg)'
    end_pattern = '(?:^.+.jpg)(.+$)'
    
    start = re.findall(start_pattern, e1)
    url = re.findall(image_url_pattern, e1)
    end = re.findall(end_pattern, e1)
    
    print(f'start: {start}')
    print(f'url: {url}')
    print(f'end: {end}')
    

    Example 1:

    start: ['the Image Path is ']
    url: ['https://ictagrisindh.gov.pk/img/inauguration1.jpg']
    end: [' the detail goes hereand the url was this and Click here to view the detail goes here']
    

    Example 2:

    start: []
    url: ['https://ictagrisindh.gov.pk/img/inauguration1.jpg']
    end: [' the detail goes hereClick here to view screenshot the detail goes here']