Telegram 机器人中的正则表达式不起作用
Regular expression in Telegram bot not working
我花了三天时间试图让我的机器人更聪明,但我不明白如何编写正则表达式。我想在用户的消息中搜索并发送回复:
import re
@bot.message_handler(content_types=['text'])
def handle_text(message):
if message.text == re.search(r'hello','Hello'):
bot.reply_to(message, "Welcome")
这不起作用,机器人只是忽略了我的消息。
你if
和re
的表情很奇怪。你想实现什么逻辑?如果你想检查 message.text
是否包含 hello
或 Hello
(确切地)你应该使用:
if re.search(r'hello|Hello', message.text):
我首先建议您阅读 re
的文档 - 它的使用有时非常微妙。
我假设您要执行的操作是 运行 函数 if message.text == "Hello"
。这样,您要查找的代码段就是
if re.search(r'Hello|hello', message.text):
#do things
我花了三天时间试图让我的机器人更聪明,但我不明白如何编写正则表达式。我想在用户的消息中搜索并发送回复:
import re
@bot.message_handler(content_types=['text'])
def handle_text(message):
if message.text == re.search(r'hello','Hello'):
bot.reply_to(message, "Welcome")
这不起作用,机器人只是忽略了我的消息。
你if
和re
的表情很奇怪。你想实现什么逻辑?如果你想检查 message.text
是否包含 hello
或 Hello
(确切地)你应该使用:
if re.search(r'hello|Hello', message.text):
我首先建议您阅读 re
的文档 - 它的使用有时非常微妙。
我假设您要执行的操作是 运行 函数 if message.text == "Hello"
。这样,您要查找的代码段就是
if re.search(r'Hello|hello', message.text):
#do things