1

这是给定的两个场景

示例 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

示例 2

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

我的代码如下

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))

结果

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

我想从起点到终点之间提取文本并从图像路径中的任何地方提取文本

任何帮助将不胜感激并提前感谢

4

1 回答 1

1
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}')

示例 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']

示例 2:

start: []
url: ['https://ictagrisindh.gov.pk/img/inauguration1.jpg']
end: [' the detail goes hereClick here to view screenshot the detail goes here']
于 2020-06-13T12:36:52.677 回答