# 整词匹配
src = "a b ab ba aa bb aab baa ccc ddd"
print "src, '%s'" % (src)
# a > c
old="a"
new = "c"
dst_1 = re.sub(r"\b%s\b" % (old), new, src)
print "%s To %s, '%s'" % (old, new, dst_1)
# aa > dd
old="aa"
new = "dd"
dst_2 = re.sub(r"\b%s\b" % (old), new, src)
print "%s To %s, '%s'" % (old, new, dst_2)
src, 'a b ab ba aa bb aab baa ccc ddd'
a To c, 'c b ab ba aa bb aab baa ccc ddd'
aa To dd, 'a b ab ba dd bb aab baa ccc ddd'
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26250550/viewspace-1661746/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/26250550/viewspace-1661746/