Python 3.x 如何在python中使用正则表达式获取显示在任何位置的文本以及图像路径?
结果是:Python 3.x 如何在python中使用正则表达式获取显示在任何位置的文本以及图像路径?,python-3.x,regex,Python 3.x,Regex,结果是: 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]|[$-_@.&+]|[!*\(
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
输出
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"
parts = re.search(r'^(?:(.*?) )?(https?://\S+) (.*)$', str_text)
print(parts.groups())
当str_text=“详细信息显示在此处,url为此时,此代码不起作用,单击此处查看详细信息显示在此处&图像路径为“@Umair:当然,不起作用,但这是另一个问题,您如何区分普通文本和部分url(没有
http://
)?不,url是正确的,我,但是为什么它没有显示http://@Umair:我明白你的意思,请看我的编辑,我已经将第一部分设置为可选。要查看评论中的http
,请用backitcks将其包围起来“`@Umair:不客气,很高兴它有帮助。如果你愿意,你可以投票决定答案;-)
('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')