【问题标题】:Discord Python - Search for regex in message.content and ignore anything before it or afterDiscord Python - 在 message.content 中搜索正则表达式并忽略它之前或之后的任何内容
【发布时间】:2018-03-22 13:22:32
【问题描述】:
async def on_message(self, message):
    streamables = re.compile(r'streamable(\.com)\/([\w-]{2,50})')
    if streamables.search(message.content) and message.channel.id == 363835464219754498:
        url = message.content
        print(url)

我希望它只显示链接,即使它之前或之后有东西。我该怎么做?

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    存储streamables.search(message.content)的结果得到匹配对象,然后调用group()获取匹配的对象。

    async def on_message(self, message):
        streamables = re.compile(r'streamable(\.com)\/([\w-]{2,50})')
        match = streamables.search(message.content)
        if match and message.channel.id == 363835464219754498:
            url = match.group()
            print(url)
    

    【讨论】:

    • 谢谢,这正是我所需要的。
    • 嘿,如果我想在代码中使用 replace() 怎么办?好像不太好用:hastebin.com/tuledeqebe.py
    • @Kzap2 你想完成什么?对于初学者,替换将不起作用,因为您的匹配在开始时不会包含“https://”(因为您的模式中没有包含它)。我也不确定你期望用这个替换会发生什么。
    • Nvm,我最后整理出来了。
    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2012-03-02
    相关资源
    最近更新 更多