[英]How to extract only the URL from the following strings using regular expressions?
我正在从一组原始数据中提取URL,我打算使用python正则表达式来做到这一点。
我试过了
(http.+)
但是,整个部分都从http开始。
输入
href =“ http://twitter.com/download/iphone” rel =“ nofollow”>适用于iPhone的Twitter
预期产量
试试这个: http[^\\"^\\s]*
假设您的所有链接都以http开头,并且遇到空白或"
这是使用方法:
import re
regexp = '''http[^\"^\s]*'''
urls = '''href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone https://vine.co/v/i6iIrBwnTFI'''
output = re.findall(regexp, urls)
output
[ ' http://twitter.com/download/iphone ', ' https://vine.co/v/i6iIrBwnTFI ']
然后,正则表达式可以是:
(http://|https://)([a-zA-Z0-9\-\._~:/\?\#\[\]@!$&'\(\)\*\+,;=]+)
在我的python解释器中,它看起来像:
>>> import re
>>> regexp = '''(http://|https://)([a-zA-Z0-9\-\._~:/\?\#\[\]@!$&'\(\)\*\+,;=]+)'''
>>> url = '''href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone https://vine.co/v/i6iIrBwnTFI'''
>>> r = re.findall(regexp, url)
>>> r
[('http://', 'twitter.com/download/iphone'), ('https://', 'vine.co/v/i6iIrBwnTFI')]
>>> [x[0]+x[1] for x in r]
['http://twitter.com/download/iphone', 'https://vine.co/v/i6iIrBwnTFI']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.