Re: 字符串中的查找问题

"Jiahua Huang" <[email protected]>
Newsgroups gmane.comp.python.chinese
Message-ID <[email protected]>
给你贴个逻辑匹配字符串好了

def match(text, andwords=[], orwords=[], notwords=[]):
    '''匹配字符串
    '''
    for notword in notwords:
        if notword in text:
            return False
    for andword in andwords:
        if andword not in text:
            return False
    for orword in orwords:
        if orword in text:
            return True
    return True


>>> match('早上好啊,这里是火星', ['早上','火星'])
True
>>> match('早上好啊,这里是火星', ['早上','火星'], ['好啊','不好'])
True
>>> match('早上好啊,这里是火星', ['早上', '火星'], ['好啊', '不好'], ['很好'])
True
>>> match('早上好啊,这里是火星', ['早上', '火星'], ['好啊', '不好'], ['好啊'])
False
>>>
_______________________________________________
python-chinese
Post: send [email protected]
Subscribe: send subscribe to [email protected]
Unsubscribe: send unsubscribe to  [email protected]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.