我试图找到任何非空的ha和ho的组合,例如hahahoho,ha,ho,hoha
chat_words = sorted(set(w for w in nltk.corpus.nps_chat.words()))
[w for w in words.words() if re.search('^[haho]+$',w)]
我得到的结果是:
['a',
'aa',
'ah',
'aha',
'aho',
'h',
'ha',
'hah',
'hao',
'ho',
'o',
'oh',
'oho',
'a']
Tags:
您的模式:
'^[haho]+$'
匹配这四个独立字符的任意组合,h,a,h,o
(3,因为h
是重复的)。如果要特别匹配ha
和ho
,必须使用|
标记相关问题 更多 >
编程相关推荐