【问题标题】:How to find all Discord Invites in a message如何在消息中找到所有 Discord 邀请
【发布时间】:2021-01-16 12:46:34
【问题描述】:

如何在消息中找到所有 Discord 服务器邀请。我以前问过类似的问题,但不知何故对我不起作用。所以我来到了这里。我在 Internet 上找不到更多关于此的信息。 “re”包可能会起作用,但正则表达式是什么?

import re

inviteregex = re.compile(r'discord(.gg|.com/invite)/.{3,10}')
links = re.findall(inviteregex, 'here is the link discord.gg/gpDcZfF')
print(links)

预期结果:

['discord.gg/gpDcZfF']

但结果是:

['.gg']

【问题讨论】:

  • 什么是正则表达式:en.wikipedia.org/wiki/Regular_expression(PS:你至少可以google一下)另外请添加你到目前为止尝试过的代码,结果是什么,预期结果是什么,错误/回溯...
  • 刚刚编辑了消息,没有错误。我也知道什么是正则表达式。但是我不确定我在这里使用的那个是否正确

标签: python python-3.x discord.py python-re


【解决方案1】:

邀请的正则表达式是下一个:

(?:https?://)?discord(?:(?:app)?\.com/invite|\.gg)/?[a-zA-Z0-9]+/?

应用于on_message 事件

async def on_message(message):
    invite_regex = re.compile("(?:https?://)?discord(?:(?:app)?\.com/invite|\.gg)/?[a-zA-Z0-9]+/?")
    result = invite_regex.findall(message.content)

    print(result)

【讨论】:

    【解决方案2】:

    这里我做了一个正则表达式,可以识别来自 dsc.gg 和 discord.io 的邀请

    (https|http)://(dsc\.gg|discord\.gg|discord\.io|dsc\.lol)/?[\S]+/?
    

    https://regex101.com/r/qUsc7J/1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 2021-10-23
      • 2017-12-24
      • 2016-06-07
      • 1970-01-01
      • 2021-07-23
      相关资源
      最近更新 更多