import re
class WordDictionary:
def __init__(self):
self.words = '#'
def addWord(self, word):
self.words += word + '#'
def search(self, word):
return bool(re.search('#' + word + '#', self.words))
#re.match只匹配字符串的开始,如果字符串开始不符合正则表达式,则匹配失败,函数返回None;#
# re.search匹配整个字符串,直到找到一个匹配
Python 正则表达式学习:
https://www.cnblogs.com/kadycui/p/10792561.html