简体   繁体   English

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

[英]Trouble searching a string for words python

I have the following string: 我有以下字符串:

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

and the following function, to search it for yes or y: 和以下函数,以在其中搜索yes或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

as far as I can tell nothing is being returned. 据我所知,什么也没有退还。 When I step through this in the pycharm debugger, it doesn't seem to get to the return statement ( very strange behaviour ). 当我在pycharm调试器中逐步执行此操作时,似乎无法进入return语句(非常奇怪的行为)。

What am I doing wrong? 我究竟做错了什么?

Your code throws a TypeError: expected string or buffer on a match_obj = to_find.search(search_str.lower) line. 您的代码在match_obj = to_find.search(search_str.lower)行上引发TypeError: expected string or buffer

lower() is a method , you need to call it: lower() 是一个方法 ,您需要调用它:

to_find.search(search_str.lower())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM