Regex pattern matching fails in Telegram bot message handler

I’ve been working on this for several days and can’t figure out what’s wrong with my regex implementation. I’m trying to detect specific words in user messages and respond accordingly:

import re
@bot.message_handler(content_types=['text'])
def process_message(msg):
    if msg.text == re.search(r'hi','Hi there'):
        bot.reply_to(msg, "Hello there")

The bot doesn’t respond at all when I send messages. What am I doing wrong with the regular expression matching?

You’re searching for ‘hi’ in the hardcoded string ‘Hi there’ instead of the actual user message. Plus, re.search() returns a Match object (or None), so your equality comparison won’t work. Try this: if re.search(r'\bhi\b', msg.text, re.IGNORECASE): The \b word boundaries stop it from matching ‘hi’ inside words like ‘this’ or ‘him’. I learned this the hard way - my bot started responding to every message with those letters. Without word boundaries, your regex gets too greedy and matches stuff you don’t want.

you’re comparing msg.text to the regex result, but that’s not right. use if re.search(r'hi', msg.text.lower()): instead - it’ll actually match the pattern in the message.

Your comparison logic is wrong. You’re checking if msg.text equals the result of searching for ‘hi’ in the string ‘Hi there’ - that’ll always be false. re.search() returns a match object or None, not a string you can compare against. You need to search the actual message text: if re.search(r'hi', msg.text, re.IGNORECASE): This searches for ‘hi’ in the user’s message, and re.IGNORECASE makes it case-insensitive so it catches both ‘hi’ and ‘Hi.’