Search code examples
pythonpython-3.xsqlitetelegrampy-telegram-bot-api

re.fullmatch sql python telegram


I need an exact comparison with records from a database. I have a string data from db:

print(str(db.get_nicknames2(message.from_user.id)))
//[('123',), ('lopr',), ('hello',), ('imfamous2',), ('guy',)]

Does not work:

message = 'famous2'
info = str(db.get_nicknames2(message.from_user.id))
re.fullmatch(message,info)
//None

It works but i need to compare from db:

message = 'famous2'
info = 'famous2'
re.fullmatch(message,info)
//re.Match object; span=(0, 9), match='imfamous2

Solution

  • I don't think you need re for this at all. Just loop over the info list instead of converting it to a string.

    info = 'famous'
    data = db.get_nicknames2(message.from_user.id)
    nickname = None
    # first look for exact match
    for (nick,) in data:
        if info == nick:
            nickname = nick
            break
    else:
        # not found, look for substring match
        for (nick,) in data:
            if info in nick:
                nickname = nick
                break