在字符串中查找URL的正则表达式

2024-04-23 23:43:09 发布

您现在位置:Python中文网/ 问答频道 /正文

Possible Duplicate:
What is the best regular expression to check if a string is a valid URL

我想从字符串中找到诸如http://www.google.com或{}之类的url。实现这一目标的最佳方法是什么?在


Tags: theto字符串urlstringifischeck
1条回答
网友
1楼 · 发布于 2024-04-23 23:43:09
>>> 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"']

请注意,最后一个示例非常简化,不应在实践中使用。如果你想这样做的话,谷歌的网址正则表达式。在

相关问题 更多 >