我如何才能识别。洋葱链接在一个文本中铭记着它们可以以多种方式出现;
hfajlhfjkdsflkdsja.onion
http://hfajlhfjkdsflkdsja.onion
http://www.hfajlhfjkdsflkdsja.onion
我在考虑regex,但是
(.*?.onion)
会返回url链接所在的整个段落 最佳答案
这将做到:(?:https?://)?(?:www)?(\S*?\.onion)\b
(添加了非捕获组-credit:@WiktorStribiżew)
演示:
s = '''hfajlhfjkdsflkdsja.onion
https://hfajlhfjkdsflkdsja.onion
http://www.hfajlhfjkdsflkdsja.onion
https://www.google.com
https://stackoverflow.com'''
for m in re.finditer(r'(?:https?://)?(?:www)?(\S*?\.onion)\b', s, re.M | re.IGNORECASE):
print(m.group(0))
输出
hfajlhfjkdsflkdsja.onion
https://hfajlhfjkdsflkdsja.onion
http://www.hfajlhfjkdsflkdsja.onion