我需要帮助。我如何从字符串中获取域名?
例如:“嗨,我是 Natsume,请查看我的网站 http://www.mysite.com/ ”
我怎样才能得到 mysite.com?
输出示例:
http://www.mysite.com/ (如果输入 http)
www.mysite.com(如果未输入 http)
mysite.com(如果未输入 http 和 www)
最佳答案
myString = "Hi im Natsume, check out my site http://www.mysite.com/"
>>> a = re.search("(?P<url>https?://[^\s]+)", myString) or re.search("(?P<url>www[^\s]+)", myString)
>>> a.group("url")
'http://www.mysite.com/'
>>> myString = "Hi im Natsume, check out my site www.mysite.com/"
>>> a = re.search("(?P<url>https?://[^\s]+)", myString) or re.search("(?P<url>www[^\s]+)", myString)
>>> a.group("url")
'www.mysite.com/'
关于python - 从字符串中获取域? - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11226722/