在字符串中搜索单词时遇到问题

2024-10-01 09:16:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下字符串:

u'>>\n> yes\n>'

以及以下函数,以搜索“是”或“y”:

def checkformatch(search_str):    

    to_find = re.compile(r'\b(yes|y)\b')
    match_obj = to_find.search(search_str.lower)
    which_word_matched = match_obj.group() if match_obj else ''
    return which_word_matched

据我所知,什么也没有归还。当我在pycharm调试器中单步执行此操作时,它似乎没有到达return语句(非常奇怪的行为)

我做错什么了


Tags: to函数字符串objwhichsearchreturndef
1条回答
网友
1楼 · 发布于 2024-10-01 09:16:32

您的代码在match_obj = to_find.search(search_str.lower)行上抛出一个TypeError: expected string or buffer

lower()is a method,您需要调用它:

to_find.search(search_str.lower())

相关问题 更多 >