Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在python中使用正则表达式获取显示在任何位置的文本以及图像路径?_Python 3.x_Regex - Fatal编程技术网

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