A regular expression to find URLs in a string [duplicate]
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.
精彩评论