import re
# create a regular expression object
regex = re.compile(r'(?:\b\w{1,}\b)|,')
# the inputs
old = "He is a new employee here."
new = "He was a new, employee there."
# creating lists of the words (or commas) in each sentence
old_words = re.findall(regex, old)
new_words = re.findall(regex, new)
# generate a list of words from new_words if it isn't in the old words
# also checking for words that previously existed but are then added
word_differences = []
for word in new_words:
if word in old_words:
old_words.remove(word)
else:
word_differences.append(word)
# print it out to verify
print word_differences
我用了googlediff补丁匹配。很好用。你知道吗
您可以使用^{} 模块来实现这一点。你知道吗
请注意,如果要添加其他标点符号(如bang或分号),则必须将其添加到正则表达式定义中。现在,它只检查单词或逗号。
相关问题 更多 >
编程相关推荐