regex
モジュールは名前付きリストをサポートしています:
import regex
def match_words(words, string):
return regex.search(r"\b\L<words>\b", string, words=words)
def match(string, include_words, exclude_words):
return (match_words(include_words, string) and
not match_words(exclude_words, string))
例:
if match("hello world how are you what are you doing",
include_words=["world", "how are"],
exclude_words=["tigers", "bye bye"]):
print('matches')
re
たとえば、標準モジュールを使用して名前付きリストを実装できます。
import re
def match_words(words, string):
re_words = '|'.join(map(re.escape, sorted(words, key=len, reverse=True)))
return re.search(r"\b(?:{words})\b".format(words=re_words), string)
+、-、および "" 文法に基づいて、含まれる単語と除外される単語のリストを作成するにはどうすればよいですか?
使用できますshlex.split()
:
import shlex
include_words, exclude_words = [], []
for word in shlex.split('+world -tigers "how are" -"bye bye"'):
(exclude_words if word.startswith('-') else include_words).append(word.lstrip('-+'))
print(include_words, exclude_words)
# -> (['world', 'how are'], ['tigers', 'bye bye'])