我发现这里的大多数帖子都是在一个文本文件中查找url的。但并不是所有的文本文件旁边都有html标记。我正在寻找一个在这两种情况下都有效的解决方案。以下正则表达式是:
'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'
regex使用下面的代码从文本文件获取url,但问题是它还需要不必要的字符,如'>;'
这是我的代码:
^{pr2}$输出示例如下:
http://saiconference.com/ficc2018/submit
http://52.21.30.170/sendy/unsubscribe/qhiz2s763l892rkps763chacs52ieqkagf8rbueme9n763jv6da/hs1ph7xt5nvdimnwwfioya/qg0qteh7cllbw8j6amo892ca>
https://www.youtube.com/watch?v=gvwyoqnztpy>
http://saiconference.com/ficc
http://saiconference.com/ficc>
http://saiconference.com/ficc2018/submit>
如您所见,有些字符(如“>;”)会导致问题。我做错什么了?在
快速解决方案,假设“>;”是最后出现的唯一字符:
url.rstrip('>')
删除单个字符串中最后出现的字符(多次)。因此,您必须遍历列表并删除字符。在
编辑:刚得到一台装有python的PC,所以在测试之后给出了一个regex的答案。在
但是,如果只有一个字符,则使用.rstrip()会更简单。在
相关问题 更多 >
编程相关推荐