is there a way to make discord.py copy a certaint part of the text sent by users

Question:

Is there any way for discord.py bot to copy a certain part of the message, for example:

The user sends print('hello') in the chat, the bot responds with the text in the brackets, in this case hello.

Asked By: v0x5

||

Answers:

Use regex

import re
from discord.ext import commands

bot = commands.Bot()

@bot.event
async def on_message(message):
    content = message.content # in this case: print('hello')
     
    # any character inside of '' (0 or more)
    result = re.search(r''[^"]*'', content).group(0)

    # remove ' around string
    result = result.strip("'")

    await message.channel.send(result)

Result

hello
Answered By: puncher
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.