开发者

A regular expression to find URLs in a string [duplicate]

This que开发者_开发问答stion already has answers here: Closed 11 years ago.

Possible Duplicate:

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

I want to find URLs such as http://www.google.com or http://mail.yahoo.com.uk from a string. What's the best approach to achieve this?


>>> 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"']

Note that the last example is extremely simplified and should not be used in practice. Google for regular expressions for URLs if you want to do that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜