Han-Gang / test

test
0 stars 0 forks source link

全字匹配 #15

Open Han-Gang opened 5 years ago

Han-Gang commented 5 years ago
import re
from functools import partial
repl_dict = {'cat': 'Garfield', 'dog': 'Oddie' }
def helper(dic, match):
    word = match.group(0)
    return dic.get(word, word)
word_re = re.compile(r'\b[a-zA-Z]+\b')
text = "dog ate the catfood and went to cat's bed to see dog dreams on caterpillars"
print word_re.sub(partial(helper, repl_dict), text)
""" my output -->
Oddie ate the catfood and went to Garfield's bed to see Oddie dreams on caterpillars
"""