0
可能重复:
What is the best regular expression to check if a string is a valid URL正则表达式找到的URL字符串中的
我想找到的网址,例如从字符串http://www.google.com
或http://mail.yahoo.com.uk
。达到此目的的最佳方法是什么?
可能重复:
What is the best regular expression to check if a string is a valid URL正则表达式找到的URL字符串中的
我想找到的网址,例如从字符串http://www.google.com
或http://mail.yahoo.com.uk
。达到此目的的最佳方法是什么?
>>> text = """I want to find url this "http://www.google.com" or "http://mail.yahoo.com.uk" from a string.
I tried different exprs but no one correct. Could anyone help me? Thanks
"""
>>> import re
>>> re.search('(http://www\\.google\\.com)', text)
<_sre.SRE_Match object at 0x02183060>
>>> _.groups()
('http://www.google.com',)
>>> re.search('(http://mail\\.yahoo\\.com\\.uk)', text)
<_sre.SRE_Match object at 0x021830A0>
>>> _.groups()
('http://mail.yahoo.com.uk',)
>>> re.findall('(http://[^"\' ]+)', text)
['http://www.google.com"', 'http://mail.yahoo.com.uk"']
请注意,最后一个例子是非常简化,不应该在实践中使用。谷歌正则表达式的网址,如果你想这样做。
这是一个子字符串搜索或验证问题? – 2011-03-30 21:22:45
你知道几乎任何东西都是有效的URL吗?语法非常灵活。 http://tools.ietf.org/html/rfc3986。该方案和路径组件是必需的,尽管路径可能为 为空。所以'ftp:'是一个合法的URL。 – 2011-03-30 21:44:29